Skip to content

Commit dc26982

Browse files
author
anewzhuang
committed
feat: 修改 imageRender,减少多余的绘制
1 parent 7bfd722 commit dc26982

File tree

2 files changed

+5
-38
lines changed

2 files changed

+5
-38
lines changed

docs/components/overview.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Layout 通过 xml 组织布局,Layout 支持的标签列表如下。
113113
| backgroundColor | string | | 背景的颜色,支持 6 位 16 进制、8 位 16 进制、rgb、rgba 四种格式的颜色 |
114114
| backgroundImage | string | | 背景图,格式为 'url(https:/www.foo.com/xxx.png)' |
115115
| backgroundImageType | string | simple | 背景图片的渲染类型,详见下方"图像渲染模式"说明 |
116-
| backgroundImageInset | string | | 背景图片九宫格设置的区域,格式为 'left top right bottom',如'10 20 30 10' |
116+
| backgroundImageInset | string | | 背景图片九宫格设置的区域,backgroundImageType为 sliced生效,格式为 'left top right bottom',如'10 20 30 10' |
117117
| opacity | number | 1 | 透明度,范围[0, 1],0表示透明,1表示不透明 |
118118
| transform | string | | transform 属性允许你旋转和缩放给定元素,目前支持的格式 `rotate(360deg)` |
119119

@@ -123,7 +123,7 @@ Layout 通过 xml 组织布局,Layout 支持的标签列表如下。
123123
| 属性 | 类型 | 默认值 | 说明 |
124124
|---------------|--------|--------|--------------------------|
125125
| imageType | string | simple | 图像的渲染类型,详见下方"图像渲染模式"说明 |
126-
| imageInset | string | | 图像九宫格设置的区域,格式为 'left top right bottom',如'15 15 15 15' |
126+
| imageInset | string | | 图像九宫格设置的区域,imageType值为sliced生效,格式为 'left top right bottom',如'15 15 15 15' |
127127

128128
#### 图像渲染模式
129129

src/common/imageRenderer.ts

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,10 @@ export class ImageRenderer {
133133
ctx.clip();
134134

135135
// 预计算完整块和边界块的数量,避免重复计算
136-
const fullCols = Math.floor(width / imgWidth);
137-
const fullRows = Math.floor(height / imgHeight);
138-
const hasPartialCol = width % imgWidth > 0;
139-
const hasPartialRow = height % imgHeight > 0;
136+
const fullCols = Math.ceil(width / imgWidth);
137+
const fullRows = Math.ceil(height / imgHeight);
140138

141-
// 1. 优先绘制所有完整的块(最常见的情况,无需边界计算)
139+
// 绘制所有的块,无需考虑边界
142140
for (let row = 0; row < fullRows; row++) {
143141
const drawY = y + row * imgHeight;
144142
for (let col = 0; col < fullCols; col++) {
@@ -147,37 +145,6 @@ export class ImageRenderer {
147145
}
148146
}
149147

150-
// 2. 处理右边界的部分列(如果存在)
151-
if (hasPartialCol) {
152-
const partialWidth = width - fullCols * imgWidth;
153-
const partialColX = x + fullCols * imgWidth;
154-
155-
// 完整行的部分列
156-
for (let row = 0; row < fullRows; row++) {
157-
const drawY = y + row * imgHeight;
158-
ctx.drawImage(img, 0, 0, partialWidth, imgHeight, partialColX, drawY, partialWidth, imgHeight);
159-
}
160-
161-
// 右下角的部分块(如果底边界也是部分的)
162-
if (hasPartialRow) {
163-
const partialHeight = height - fullRows * imgHeight;
164-
const partialRowY = y + fullRows * imgHeight;
165-
ctx.drawImage(img, 0, 0, partialWidth, partialHeight, partialColX, partialRowY, partialWidth, partialHeight);
166-
}
167-
}
168-
169-
// 3. 处理底边界的部分行(如果存在)
170-
if (hasPartialRow) {
171-
const partialHeight = height - fullRows * imgHeight;
172-
const partialRowY = y + fullRows * imgHeight;
173-
174-
// 完整列的部分行
175-
for (let col = 0; col < fullCols; col++) {
176-
const drawX = x + col * imgWidth;
177-
ctx.drawImage(img, 0, 0, imgWidth, partialHeight, drawX, partialRowY, imgWidth, partialHeight);
178-
}
179-
}
180-
181148
ctx.restore();
182149
}
183150

0 commit comments

Comments
 (0)