提交 e8202669 编写于 作者: H hdx

feat(canvas): 新增 canvas 绘画

上级 d2f0ead2
<template> <template>
<view> <view class="page">
<page-head :title="title"></page-head> <page-head :title="title"></page-head>
<view class="uni-common-mt"> <canvas class="canvas-element" canvas-id="canvas" id="canvas"></canvas>
<canvas class="canvas-element" canvas-id="canvas" id="canvas"></canvas> <scroll-view class="scroll-view" :scroll-y="true">
<scroll-view class="canvas-buttons" scroll-y="true"> <view class="grid-list">
<block v-for="(name, index) in names" :key="index"> <view class="grid-item" v-for="(name, index) in names" :key="index">
<button class="canvas-button" @click="handleCanvasButton(name)">{{name}}</button> <button class="canvas-button" @click="handleCanvasButton(name)">{{name}}</button>
</block> </view>
<button class="canvas-button" @click="toTempFilePath" type="primary">toTempFilePath</button> </view>
</scroll-view> <button class="canvas-drawing-button" @click="toTempFilePath" type="primary">toTempFilePath</button>
</view> </scroll-view>
</view> </view>
</template> </template>
<script> <script>
var context = null;
export default { export default {
data() { data() {
return { return {
...@@ -22,322 +22,418 @@ ...@@ -22,322 +22,418 @@
"stroke", "clearRect", "beginPath", "closePath", "moveTo", "lineTo", "rect", "arc", "stroke", "clearRect", "beginPath", "closePath", "moveTo", "lineTo", "rect", "arc",
"quadraticCurveTo", "bezierCurveTo", "setFillStyle", "setStrokeStyle", "setGlobalAlpha", "quadraticCurveTo", "bezierCurveTo", "setFillStyle", "setStrokeStyle", "setGlobalAlpha",
"setShadow", "setFontSize", "setLineCap", "setLineJoin", "setLineWidth", "setMiterLimit" "setShadow", "setFontSize", "setLineCap", "setLineJoin", "setLineWidth", "setMiterLimit"
] ],
// TODO 缺失 CanvasContext
canvasContext: null as any | null
} }
}, },
onReady: function () { onReady() {
context = uni.createCanvasContext('canvas', this) // @ts-ignore
this.canvasContext = uni.createCanvasContext('canvas', this)
}, },
methods: { methods: {
toTempFilePath: function () { toTempFilePath() {
uni.canvasToTempFilePath({ // TODO 缺失
canvasId: 'canvas', // uni.canvasToTempFilePath({
success: function (res) { // canvasId: 'canvas',
console.log(res.tempFilePath) // success: (res) => {
}, // console.log(res.tempFilePath)
fail: function (err) { // },
console.error(JSON.stringify(err)) // fail: (err) => {
} // console.error(JSON.stringify(err))
}) // }
// })
}, },
handleCanvasButton: function (name) { handleCanvasButton(name : string) {
this[name] && this[name](); switch (name) {
case "rotate":
this.rotate();
break;
case "scale":
this.scale();
break;
case "reset":
this.reset();
break;
case "translate":
this.translate();
break;
case "save":
this.save();
break;
case "restore":
this.restore();
break;
case "drawImage":
this.drawImage();
break;
case "fillText":
this.fillText();
break;
case "fill":
this.fill();
break;
case "stroke":
this.stroke();
break;
case "clearRect":
this.clearRect();
break;
case "beginPath":
this.beginPath();
break;
case "closePath":
this.closePath();
break;
case "moveTo":
this.moveTo();
break;
case "lineTo":
this.lineTo();
break;
case "rect":
this.rect();
break;
case "arc":
this.arc();
break;
case "quadraticCurveTo":
this.quadraticCurveTo();
break;
case "bezierCurveTo":
this.bezierCurveTo();
break;
case "setFillStyle":
this.setFillStyle();
break;
case "setStrokeStyle":
this.setStrokeStyle();
break;
case "setGlobalAlpha":
this.setGlobalAlpha();
break;
case "setShadow":
this.setShadow();
break;
case "setFontSize":
this.setFontSize();
break;
case "setLineCap":
this.setLineCap();
break;
case "setLineJoin":
this.setLineJoin();
break;
case "setLineWidth":
this.setLineWidth();
break;
case "setMiterLimit":
this.setMiterLimit();
break;
default:
break;
}
}, },
rotate: function () { rotate() {
context.beginPath() this.canvasContext!.beginPath()
context.rotate(10 * Math.PI / 180) this.canvasContext!.rotate(10 * Math.PI / 180)
context.rect(225, 75, 20, 10) this.canvasContext!.rect(225, 75, 20, 10)
context.fill() this.canvasContext!.fill()
context.draw() this.canvasContext!.draw()
}, },
scale: function () { scale() {
context.beginPath() this.canvasContext!.beginPath()
context.rect(25, 25, 50, 50) this.canvasContext!.rect(25, 25, 50, 50)
context.stroke() this.canvasContext!.stroke()
context.scale(2, 2) this.canvasContext!.scale(2, 2)
context.beginPath() this.canvasContext!.beginPath()
context.rect(25, 25, 50, 50) this.canvasContext!.rect(25, 25, 50, 50)
context.stroke() this.canvasContext!.stroke()
context.draw() this.canvasContext!.draw()
}, },
reset: function () { reset() {
context.beginPath() this.canvasContext!.beginPath()
context.setFillStyle('#000000') this.canvasContext!.setFillStyle('#000000')
context.setStrokeStyle('#000000') this.canvasContext!.setStrokeStyle('#000000')
context.setFontSize(10) this.canvasContext!.setFontSize(10)
context.setGlobalAlpha(1) this.canvasContext!.setGlobalAlpha(1)
context.setShadow(0, 0, 0, 'rgba(0, 0, 0, 0)') this.canvasContext!.setShadow(0, 0, 0, 'rgba(0, 0, 0, 0)')
context.setLineCap('butt') this.canvasContext!.setLineCap('butt')
context.setLineJoin('miter') this.canvasContext!.setLineJoin('miter')
context.setLineWidth(1) this.canvasContext!.setLineWidth(1)
context.setMiterLimit(10) this.canvasContext!.setMiterLimit(10)
context.draw() this.canvasContext!.draw()
}, },
translate: function () { translate() {
context.beginPath() this.canvasContext!.beginPath()
context.rect(10, 10, 100, 50) this.canvasContext!.rect(10, 10, 100, 50)
context.fill() this.canvasContext!.fill()
context.translate(70, 70) this.canvasContext!.translate(70, 70)
context.beginPath() this.canvasContext!.beginPath()
context.fill() this.canvasContext!.fill()
context.draw() this.canvasContext!.draw()
}, },
save: function () { save() {
context.beginPath() this.canvasContext!.beginPath()
context.setStrokeStyle('#00ff00') this.canvasContext!.setStrokeStyle('#00ff00')
context.save() this.canvasContext!.save()
context.scale(2, 2) this.canvasContext!.scale(2, 2)
context.setStrokeStyle('#ff0000') this.canvasContext!.setStrokeStyle('#ff0000')
context.rect(0, 0, 100, 100) this.canvasContext!.rect(0, 0, 100, 100)
context.stroke() this.canvasContext!.stroke()
context.restore() this.canvasContext!.restore()
context.rect(0, 0, 50, 50) this.canvasContext!.rect(0, 0, 50, 50)
context.stroke() this.canvasContext!.stroke()
context.draw() this.canvasContext!.draw()
}, },
restore: function () { restore() {
[3, 2, 1].forEach(function (item) { [3, 2, 1].forEach((item) => {
context.beginPath() this.canvasContext!.beginPath()
context.save() this.canvasContext!.save()
context.scale(item, item) this.canvasContext!.scale(item, item)
context.rect(10, 10, 100, 100) this.canvasContext!.rect(10, 10, 100, 100)
context.stroke() this.canvasContext!.stroke()
context.restore() this.canvasContext!.restore()
}); });
context.draw() this.canvasContext!.draw()
}, },
drawImage: function () { drawImage() {
// #ifdef APP-PLUS // #ifdef APP-PLUS
context.drawImage('../../../static/app-plus/uni@2x.png', 0, 0) this.canvasContext!.drawImage('../../../static/app-plus/uni@2x.png', 0, 0)
// #endif // #endif
// #ifndef APP-PLUS // #ifndef APP-PLUS
context.drawImage('../../../static/uni.png', 0, 0) this.canvasContext!.drawImage('../../../static/uni.png', 0, 0)
// #endif // #endif
context.draw() this.canvasContext!.draw()
}, },
fillText: function () { fillText() {
context.setStrokeStyle('#ff0000') this.canvasContext!.setStrokeStyle('#ff0000')
context.beginPath() this.canvasContext!.beginPath()
context.moveTo(0, 10) this.canvasContext!.moveTo(0, 10)
context.lineTo(300, 10) this.canvasContext!.lineTo(300, 10)
context.stroke() this.canvasContext!.stroke()
// context.save() // this.canvasContext!.save()
// context.scale(1.5, 1.5) // this.canvasContext!.scale(1.5, 1.5)
// context.translate(20, 20) // this.canvasContext!.translate(20, 20)
context.setFontSize(10) this.canvasContext!.setFontSize(10)
context.fillText('Hello World', 0, 30) this.canvasContext!.fillText('Hello World', 0, 30)
context.setFontSize(20) this.canvasContext!.setFontSize(20)
context.fillText('Hello World', 100, 30) this.canvasContext!.fillText('Hello World', 100, 30)
// context.restore() // this.canvasContext!.restore()
context.beginPath() this.canvasContext!.beginPath()
context.moveTo(0, 30) this.canvasContext!.moveTo(0, 30)
context.lineTo(300, 30) this.canvasContext!.lineTo(300, 30)
context.stroke() this.canvasContext!.stroke()
context.draw() this.canvasContext!.draw()
}, },
fill: function () { fill() {
context.beginPath() this.canvasContext!.beginPath()
context.rect(20, 20, 150, 100) this.canvasContext!.rect(20, 20, 150, 100)
context.setStrokeStyle('#00ff00') this.canvasContext!.setStrokeStyle('#00ff00')
context.fill() this.canvasContext!.fill()
context.draw() this.canvasContext!.draw()
}, },
stroke: function () { stroke() {
context.beginPath() this.canvasContext!.beginPath()
context.moveTo(20, 20) this.canvasContext!.moveTo(20, 20)
context.lineTo(20, 100) this.canvasContext!.lineTo(20, 100)
context.lineTo(70, 100) this.canvasContext!.lineTo(70, 100)
context.setStrokeStyle('#00ff00') this.canvasContext!.setStrokeStyle('#00ff00')
context.stroke() this.canvasContext!.stroke()
context.draw() this.canvasContext!.draw()
}, },
clearRect: function () { clearRect() {
context.setFillStyle('#ff0000') this.canvasContext!.setFillStyle('#ff0000')
context.beginPath() this.canvasContext!.beginPath()
context.rect(0, 0, 300, 150) this.canvasContext!.rect(0, 0, 300, 150)
context.fill() this.canvasContext!.fill()
context.clearRect(20, 20, 100, 50) this.canvasContext!.clearRect(20, 20, 100, 50)
context.draw() this.canvasContext!.draw()
}, },
beginPath: function () { beginPath() {
context.beginPath() this.canvasContext!.beginPath()
context.setLineWidth(5) this.canvasContext!.setLineWidth(5)
context.setStrokeStyle('#ff0000') this.canvasContext!.setStrokeStyle('#ff0000')
context.moveTo(0, 75) this.canvasContext!.moveTo(0, 75)
context.lineTo(250, 75) this.canvasContext!.lineTo(250, 75)
context.stroke() this.canvasContext!.stroke()
context.beginPath() this.canvasContext!.beginPath()
context.setStrokeStyle('#0000ff') this.canvasContext!.setStrokeStyle('#0000ff')
context.moveTo(50, 0) this.canvasContext!.moveTo(50, 0)
context.lineTo(150, 130) this.canvasContext!.lineTo(150, 130)
context.stroke() this.canvasContext!.stroke()
context.draw() this.canvasContext!.draw()
}, },
closePath: function () { closePath() {
context.beginPath() this.canvasContext!.beginPath()
context.setLineWidth(1) this.canvasContext!.setLineWidth(1)
context.moveTo(20, 20) this.canvasContext!.moveTo(20, 20)
context.lineTo(20, 100) this.canvasContext!.lineTo(20, 100)
context.lineTo(70, 100) this.canvasContext!.lineTo(70, 100)
context.closePath() this.canvasContext!.closePath()
context.stroke() this.canvasContext!.stroke()
context.draw() this.canvasContext!.draw()
}, },
moveTo: function () { moveTo() {
context.beginPath() this.canvasContext!.beginPath()
context.moveTo(0, 0) this.canvasContext!.moveTo(0, 0)
context.lineTo(300, 150) this.canvasContext!.lineTo(300, 150)
context.stroke() this.canvasContext!.stroke()
context.draw() this.canvasContext!.draw()
}, },
lineTo: function () { lineTo() {
context.beginPath() this.canvasContext!.beginPath()
context.moveTo(20, 20) this.canvasContext!.moveTo(20, 20)
context.lineTo(20, 100) this.canvasContext!.lineTo(20, 100)
context.lineTo(70, 100) this.canvasContext!.lineTo(70, 100)
context.stroke() this.canvasContext!.stroke()
context.draw() this.canvasContext!.draw()
}, },
rect: function () { rect() {
context.beginPath() this.canvasContext!.beginPath()
context.rect(20, 20, 150, 100) this.canvasContext!.rect(20, 20, 150, 100)
context.stroke() this.canvasContext!.stroke()
context.draw() this.canvasContext!.draw()
}, },
arc: function () { arc() {
context.beginPath() this.canvasContext!.beginPath()
context.setLineWidth(2) this.canvasContext!.setLineWidth(2)
context.arc(75, 75, 50, 0, Math.PI * 2, true) this.canvasContext!.arc(75, 75, 50, 0, Math.PI * 2, true)
context.moveTo(110, 75) this.canvasContext!.moveTo(110, 75)
context.arc(75, 75, 35, 0, Math.PI, false) this.canvasContext!.arc(75, 75, 35, 0, Math.PI, false)
context.moveTo(65, 65) this.canvasContext!.moveTo(65, 65)
context.arc(60, 65, 5, 0, Math.PI * 2, true) this.canvasContext!.arc(60, 65, 5, 0, Math.PI * 2, true)
context.moveTo(95, 65) this.canvasContext!.moveTo(95, 65)
context.arc(90, 65, 5, 0, Math.PI * 2, true) this.canvasContext!.arc(90, 65, 5, 0, Math.PI * 2, true)
context.stroke() this.canvasContext!.stroke()
context.draw() this.canvasContext!.draw()
}, },
quadraticCurveTo: function () { quadraticCurveTo() {
context.beginPath() this.canvasContext!.beginPath()
context.moveTo(20, 20) this.canvasContext!.moveTo(20, 20)
context.quadraticCurveTo(20, 100, 200, 20) this.canvasContext!.quadraticCurveTo(20, 100, 200, 20)
context.stroke() this.canvasContext!.stroke()
context.draw() this.canvasContext!.draw()
}, },
bezierCurveTo: function () { bezierCurveTo() {
context.beginPath() this.canvasContext!.beginPath()
context.moveTo(20, 20) this.canvasContext!.moveTo(20, 20)
context.bezierCurveTo(20, 100, 200, 100, 200, 20) this.canvasContext!.bezierCurveTo(20, 100, 200, 100, 200, 20)
context.stroke() this.canvasContext!.stroke()
context.draw() this.canvasContext!.draw()
}, },
setFillStyle: function () { setFillStyle() {
['#fef957', 'rgb(242,159,63)', 'rgb(242,117,63)', '#e87e51'].forEach(function (item, index) { ['#fef957', 'rgb(242,159,63)', 'rgb(242,117,63)', '#e87e51'].forEach((item : string, index : number) => {
context.setFillStyle(item) this.canvasContext!.setFillStyle(item)
context.beginPath() this.canvasContext!.beginPath()
context.rect(0 + 75 * index, 0, 50, 50) this.canvasContext!.rect(0 + 75 * index, 0, 50, 50)
context.fill() this.canvasContext!.fill()
}) })
context.draw() this.canvasContext!.draw()
}, },
setStrokeStyle: function () { setStrokeStyle() {
['#fef957', 'rgb(242,159,63)', 'rgb(242,117,63)', '#e87e51'].forEach(function (item, index) { ['#fef957', 'rgb(242,159,63)', 'rgb(242,117,63)', '#e87e51'].forEach((item : string, index : number) => {
context.setStrokeStyle(item) this.canvasContext!.setStrokeStyle(item)
context.beginPath() this.canvasContext!.beginPath()
context.rect(0 + 75 * index, 0, 50, 50) this.canvasContext!.rect(0 + 75 * index, 0, 50, 50)
context.stroke() this.canvasContext!.stroke()
}) })
context.draw() this.canvasContext!.draw()
}, },
setGlobalAlpha: function () { setGlobalAlpha() {
context.setFillStyle('#000000'); this.canvasContext!.setFillStyle('#000000');
[1, 0.5, 0.1].forEach(function (item, index) { [1, 0.5, 0.1].forEach((item : number, index : number) => {
context.setGlobalAlpha(item) this.canvasContext!.setGlobalAlpha(item)
context.beginPath() this.canvasContext!.beginPath()
context.rect(0 + 75 * index, 0, 50, 50) this.canvasContext!.rect(0 + 75 * index, 0, 50, 50)
context.fill() this.canvasContext!.fill()
}) })
context.draw() this.canvasContext!.draw()
context.setGlobalAlpha(1) this.canvasContext!.setGlobalAlpha(1)
}, },
setShadow: function () { setShadow() {
context.beginPath() this.canvasContext!.beginPath()
context.setShadow(10, 10, 10, 'rgba(0, 0, 0, 199)') this.canvasContext!.setShadow(10, 10, 10, 'rgba(0, 0, 0, 199)')
context.rect(10, 10, 100, 100) this.canvasContext!.rect(10, 10, 100, 100)
context.fill() this.canvasContext!.fill()
context.draw() this.canvasContext!.draw()
}, },
setFontSize: function () { setFontSize() {
[10, 20, 30, 40].forEach(function (item, index) { [10, 20, 30, 40].forEach((item : number, index : number) => {
context.setFontSize(item) this.canvasContext!.setFontSize(item)
context.fillText('Hello, world', 20, 20 + 40 * index) this.canvasContext!.fillText('Hello, world', 20, 20 + 40 * index)
}) })
context.draw() this.canvasContext!.draw()
}, },
setLineCap: function () { setLineCap() {
context.setLineWidth(10); this.canvasContext!.setLineWidth(10);
['butt', 'round', 'square'].forEach(function (item, index) { ['butt', 'round', 'square'].forEach((item : string, index : number) => {
context.beginPath() this.canvasContext!.beginPath()
context.setLineCap(item) this.canvasContext!.setLineCap(item)
context.moveTo(20, 20 + 20 * index) this.canvasContext!.moveTo(20, 20 + 20 * index)
context.lineTo(100, 20 + 20 * index) this.canvasContext!.lineTo(100, 20 + 20 * index)
context.stroke() this.canvasContext!.stroke()
}) })
context.draw() this.canvasContext!.draw()
}, },
setLineJoin: function () { setLineJoin() {
context.setLineWidth(10); this.canvasContext!.setLineWidth(10);
['bevel', 'round', 'miter'].forEach(function (item, index) { ['bevel', 'round', 'miter'].forEach((item : string, index : number) => {
context.beginPath() this.canvasContext!.beginPath()
context.setLineJoin(item) this.canvasContext!.setLineJoin(item)
context.moveTo(20 + 80 * index, 20) this.canvasContext!.moveTo(20 + 80 * index, 20)
context.lineTo(100 + 80 * index, 50) this.canvasContext!.lineTo(100 + 80 * index, 50)
context.lineTo(20 + 80 * index, 100) this.canvasContext!.lineTo(20 + 80 * index, 100)
context.stroke() this.canvasContext!.stroke()
}) })
context.draw() this.canvasContext!.draw()
}, },
setLineWidth: function () { setLineWidth() {
[2, 4, 6, 8, 10].forEach(function (item, index) { [2, 4, 6, 8, 10].forEach((item : number, index : number) => {
context.beginPath() this.canvasContext!.beginPath()
context.setLineWidth(item) this.canvasContext!.setLineWidth(item)
context.moveTo(20, 20 + 20 * index) this.canvasContext!.moveTo(20, 20 + 20 * index)
context.lineTo(100, 20 + 20 * index) this.canvasContext!.lineTo(100, 20 + 20 * index)
context.stroke() this.canvasContext!.stroke()
}) })
context.draw() this.canvasContext!.draw()
}, },
setMiterLimit: function () { setMiterLimit() {
context.setLineWidth(4); this.canvasContext!.setLineWidth(4);
[2, 4, 6, 8, 10].forEach(function (item, index) { [2, 4, 6, 8, 10].forEach((item : number, index : number) => {
context.beginPath() this.canvasContext!.beginPath()
context.setMiterLimit(item) this.canvasContext!.setMiterLimit(item)
context.moveTo(20 + 80 * index, 20) this.canvasContext!.moveTo(20 + 80 * index, 20)
context.lineTo(100 + 80 * index, 50) this.canvasContext!.lineTo(100 + 80 * index, 50)
context.lineTo(20 + 80 * index, 100) this.canvasContext!.lineTo(20 + 80 * index, 100)
context.stroke() this.canvasContext!.stroke()
}) })
context.draw() this.canvasContext!.draw()
} }
} }
} }
</script> </script>
<style> <style>
.canvas-element-wrapper { .page {
display: block; flex: 1;
margin-bottom: 100rpx; height: 100%;
overflow: hidden;
}
.scroll-view {
flex: 1;
} }
.canvas-element { .canvas-element {
...@@ -346,21 +442,16 @@ ...@@ -346,21 +442,16 @@
background-color: #ffffff; background-color: #ffffff;
} }
.canvas-buttons { .grid-list {
padding: 30rpx 50rpx 10rpx; padding: 15px;
width: 100%; flex-direction: row;
height: 360rpx; flex-wrap: wrap;
box-sizing: border-box; box-sizing: border-box;
} }
.canvas-button { .grid-item {
float: left; box-sizing: border-box;
display: inline-flex; width: 50%;
align-items: center; padding: 5px;
justify-content: center;
height: 40px;
line-height: 1;
width: 300rpx;
margin: 15rpx 12rpx;
} }
</style> </style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册