tencent cloud

Feedback

Last updated: 2024-03-04 23:04:11
Feature Description: Canvas. This component is a native component, so mind the relevant restrictions when using it.
Note:
For related APIs, see wx.createcameracontext.
Avoid setting excessively large width and height values, as this may cause crash issues in the Android environment.
Attribute name
Type
Default value
Note
canvas-id
string
-
Unique identifier of the canvas component
disable-scroll
boolean
false
When moving within the canvas and a gesture event is bound, screen scrolling and pull-down refresh are prevented.
bindtouchstart
eventhandle
-
Initiation of finger touch action
bindtouchmove
eventhandle
-
Movement after finger touch
bindtouchend
eventhandle
-
Termination of finger touch action
bindtouchcancel
eventhandle
-
Finger touch action is interrupted, such as by an incoming call alert.
bindlongtap
eventhandle
-
Upon a finger's prolonged contact of 500 ms, a long-press event is triggered. Following this, movement will not trigger screen scrolling.
binderror
eventhandle
-
Upon encountering an error, an 'error' event is triggered, with detail = {errMsg: 'something wrong'}.
Note:
The default dimensions of the canvas tag are 300 px in width and 150 px in height.
Within the same page, the canvas-id must be unique. If a previously used canvas-id is employed, the corresponding canvas tag's canvas will be hidden and cease to function properly.
Sample Code
<!-- canvas.wxml -->
<canvas style="width: 300px; height: 200px;" canvas-id="firstCanvas"></canvas>
<!-- When absolute positioning is used, the display level of the canvas following the document flow is superior to that of the preceding canvas -->
<canvas style="width: 400px; height: 500px;" canvas-id="secondCanvas"></canvas>
<!-- As the canvas-id is duplicated from the previous canvas, this canvas will not be displayed, and an error event will be dispatched to the AppService -->
<canvas
style="width: 400px; height: 500px;"
canvas-id="secondCanvas"
binderror="canvasIdErrorCallback"
></canvas>
// canvas.js
Page({
canvasIdErrorCallback(e) {
console.error(e.detail.errMsg)
},
onReady(e) {
// Use wx.createContext to obtain the drawing context
const context = wx.createCanvasContext('firstCanvas')

context.setStrokeStyle('#00ff00')
context.setLineWidth(5)
context.rect(0, 0, 200, 200)
context.stroke()
context.setStrokeStyle('#ff0000')
context.setLineWidth(2)
context.moveTo(160, 100)
context.arc(100, 100, 60, 0, 2 * Math.PI, true)
context.moveTo(140, 100)
context.arc(100, 100, 40, 0, Math.PI, false)
context.moveTo(85, 80)
context.arc(80, 80, 5, 0, 2 * Math.PI, true)
context.moveTo(125, 80)
context.arc(120, 80, 5, 0, 2 * Math.PI, true)
context.stroke()
context.draw()
}
})

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

7x24 Phone Support