提交 f67cf296 编写于 作者: 辛宝Otto's avatar 辛宝Otto 🥊

feat: 添加 canvas CreatePath2D 相关测试

上级 4135e061
...@@ -40,10 +40,22 @@ describe('Canvas.uvue', () => { ...@@ -40,10 +40,22 @@ describe('Canvas.uvue', () => {
expect(await element.text()).toBe('true') expect(await element.text()).toBe('true')
}) })
// 配合安卓注释 // 配合安卓注释
// it('测试 createImage', async () => { it('测试 createImage', async () => {
// await page.callMethod('onCreateImage'); if (process.env.uniTestPlatformInfo.toLowerCase().startsWith('web')) {
// await page.waitFor(500) // 加载图片 // web skip
// const element = await page.$('#testCreateImage') expect(true).toBe(true)
// expect(await element.text()).toBe('true') } else {
// }) await page.callMethod('onCreateImage');
await page.waitFor(500) // 加载图片
const element = await page.$('#testCreateImage')
expect(await element.text()).toBe('true')
}
})
it('测试 testCreatePath2D', async () => {
// onCreatePath2D
await page.callMethod('onCreatePath2D');
await page.waitFor(50)
const element = await page.$('#testCreatePath2D')
expect(await element.text()).toBe('true')
})
}) })
...@@ -20,6 +20,11 @@ ...@@ -20,6 +20,11 @@
<text id="testCreateImage">{{testCreateImage}}</text> <text id="testCreateImage">{{testCreateImage}}</text>
</view> </view>
<view class="text-group">
<text>测试 createPath2D 结果:</text>
<text id="testCreatePath2D">{{testCreatePath2D}}</text>
</view>
<!-- #ifdef WEB --> <!-- #ifdef WEB -->
<button class="canvas-drawing-button" @click="canvasToBlob">canvasToBlob</button> <button class="canvas-drawing-button" @click="canvasToBlob">canvasToBlob</button>
<view> <view>
...@@ -32,6 +37,7 @@ ...@@ -32,6 +37,7 @@
<text>{{dataBase64.slice(0,22)}}...</text> <text>{{dataBase64.slice(0,22)}}...</text>
</view> </view>
<button @click="onCreateImage">createImage</button> <button @click="onCreateImage">createImage</button>
<button @click="onCreatePath2D">createPath2D</button>
<view style="padding: 8px 10px;">CanvasContext API 演示</view> <view style="padding: 8px 10px;">CanvasContext API 演示</view>
<view class="grid-view"> <view class="grid-view">
<view class="grid-item" v-for="(name, index) in names" :key="index"> <view class="grid-item" v-for="(name, index) in names" :key="index">
...@@ -75,7 +81,8 @@ ...@@ -75,7 +81,8 @@
names: [...API_PATH, ...API_DRAW, ...API_STATE, ...API_PROPERTIES, "measureText", "path2D"] as string[], names: [...API_PATH, ...API_DRAW, ...API_STATE, ...API_PROPERTIES, "measureText", "path2D"] as string[],
// 仅测试 // 仅测试
testCreateCanvasContextAsyncSuccess: false, testCreateCanvasContextAsyncSuccess: false,
testCreateImage: false testCreateImage: false,
testCreatePath2D: false
} }
}, },
onReady() { onReady() {
...@@ -122,13 +129,35 @@ ...@@ -122,13 +129,35 @@
canvasToDataURL() { canvasToDataURL() {
this.dataBase64 = this.canvasContext!.toDataURL() this.dataBase64 = this.canvasContext!.toDataURL()
}, },
onCreateImage() { onCreateImage() {
// let image = this.canvasContext!.createImage(); this.renderingContext!.clearRect(0, 0, this.canvasWidth, this.canvasHeight)
// image.src = "../../static/logo.png" let image = this.canvasContext!.createImage();
// image.onload = () => { image.src = "../../static/logo.png"
// this.testCreateImage = true image.onload = () => {
// this.renderingContext?.drawImage(image, 0, 0, 100, 100); this.testCreateImage = true
// } this.renderingContext?.drawImage(image, 0, 0, 100, 100);
}
},
onCreatePath2D() {
this.renderingContext!.clearRect(0, 0, this.canvasWidth, this.canvasHeight)
const context = this.renderingContext!
let path2D = this.canvasContext!.createPath2D()
this.testCreatePath2D = true
const amplitude = 64;
const wavelength = 64;
for (let i = 0; i < 5; i++) {
const x1 = 0 + (i * wavelength);
const y1 = 128;
const x2 = x1 + wavelength / 4;
const y2 = y1 - amplitude;
const x3 = x1 + 3 * wavelength / 4;
const y3 = y1 + amplitude;
const x4 = x1 + wavelength;
const y4 = y1;
context.moveTo(x1, y1);
path2D.bezierCurveTo(x2, y2, x3, y3, x4, y4);
}
context.stroke(path2D);
}, },
handleCanvasButton(name : string) { handleCanvasButton(name : string) {
this.renderingContext!.clearRect(0, 0, this.canvasWidth, this.canvasHeight) this.renderingContext!.clearRect(0, 0, this.canvasWidth, this.canvasHeight)
...@@ -255,10 +284,10 @@ ...@@ -255,10 +284,10 @@
this.setMiterLimit(); this.setMiterLimit();
break; break;
case "textAlign": case "textAlign":
this.textAlign(); this.textAlign();
break; break;
case "path2D": case "path2D":
this.path2DMethods(); this.path2DMethods();
break; break;
default: default:
break; break;
...@@ -828,31 +857,31 @@ ...@@ -828,31 +857,31 @@
const textMetrics = context.measureText(text) const textMetrics = context.measureText(text)
context.strokeText(text, 40, 100) context.strokeText(text, 40, 100)
context.fillText("measure text width:" + textMetrics.width, 40, 80) context.fillText("measure text width:" + textMetrics.width, 40, 80)
}, },
path2DMethods() { path2DMethods() {
const context = this.renderingContext! const context = this.renderingContext!
context.beginPath() context.beginPath()
const path2D = new Path2D(); const path2D = new Path2D();
const amplitude = 64; const amplitude = 64;
const wavelength = 64; const wavelength = 64;
for (let i = 0; i < 5; i++) { for (let i = 0; i < 5; i++) {
const x1 = 0 + (i * wavelength); const x1 = 0 + (i * wavelength);
const y1 = 128; const y1 = 128;
const x2 = x1 + wavelength / 4; const x2 = x1 + wavelength / 4;
const y2 = y1 - amplitude; const y2 = y1 - amplitude;
const x3 = x1 + 3 * wavelength / 4; const x3 = x1 + 3 * wavelength / 4;
const y3 = y1 + amplitude; const y3 = y1 + amplitude;
const x4 = x1 + wavelength; const x4 = x1 + wavelength;
const y4 = y1; const y4 = y1;
context.moveTo(x1, y1); context.moveTo(x1, y1);
path2D.bezierCurveTo(x2, y2, x3, y3, x4, y4); path2D.bezierCurveTo(x2, y2, x3, y3, x4, y4);
} }
context.stroke(path2D); context.stroke(path2D);
const path2DRect = new Path2D(); const path2DRect = new Path2D();
path2DRect.rect(10, 10, 100, 20); path2DRect.rect(10, 10, 100, 20);
context.fill(path2DRect); context.fill(path2DRect);
} }
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册