class Pos{ x:number = 0 y:number = 0 }
@State posLast:number[] = [0,0] @State posNow:number[] = [0,0] @State points:Pos[] = [] settings:RenderingContextSettings = new RenderingContextSettings(true) canvas:CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings)
Canvas(this.canvas) .width("100%") .height("100%") .onReady(()=>{ this.canvas.stroke() this.canvas.strokeStyle = "#a976b2" this.canvas.strokeRect(100,100,100,100) this.canvas.fillRect(200,200,100,100) }) .onTouch((event)=>{ switch(event.type){ case TouchType.Down: this.posLast[0] = event.touches[0].x this.posLast[1] = event.touches[0].y this.draw(event.touches[0].x,event.touches[0].y) break case TouchType.Up: this.points = [] break case TouchType.Move: this.posNow[0] = event.touches[0].x this.posNow[1] = event.touches[0].y break case TouchType.Cancel: break } }) draw(x:number,y:number){ this.points.push({ x:x,y:y }) if(this.points.length<1)return if(this.points.length>3){ this.posNow[0] = (this.points[this.points.length-1].x+this.points[this.points.length-2].x)/2 this.posNow[1] = (this.points[this.points.length-1].y+this.points[this.points.length-2].y)/2 let controlX = this.points[this.points.length-2].x let controlY = this.points[this.points.length-2].y
this.posLast[0] = (this.points[this.points.length-2].x+this.points[this.points.length-3].x)/2 this.posLast[1] = (this.points[this.points.length-2].y+this.points[this.points.length-3].y)/2 this.canvas.moveTo(this.posLast[0],this.posLast[1]) this.canvas.quadraticCurveTo(controlX,controlY,this.posNow[0],this.posNow[1]) this.canvas.stroke() this.canvas.lineWidth = 5 this.canvas.strokeStyle = "#a976b2" } else{} }
|