WeChat Mini Program API Drawing
wx.createContext()
Create and return the drawing context context object.
context
Context is just a container that records method calls and is used to generate an actions array that records drawing behavior. There is no corresponding relationship between context and <canvas/>
. The drawing action array of a context-generated canvas can be applied to multiple <canvas/>
.
// 假设页面上有3个画布 var canvas1Id = 3001; var canvas2Id = 3002; var canvas3Id = 3003; var context = wx.createContext(); [canvas1Id, canvas2Id, canvas3Id].forEach(function (id) { context.clearActions(); // 在context上调用方法 wx.drawCanvas({ canvasId: id, actions: context.getActions(); }); });
Method list of context object:
Method | Parameters | Description |
---|---|---|
getActions | None | Get the drawing actions stored on the current context |
clearActions | None | Clear the current stored drawing actions |
Transformation | ||
scale | Yes Scale the horizontal and vertical coordinates | |
rotate | Rotate the coordinate axis clockwise | |
translate | Scale the coordinate origin | |
save | None | Save the scaling of the current coordinate axis, Rotation and translation information |
restore | None | Restore the previously saved scaling, rotation and translation information of the coordinate axes |
Draw | ||
clearRect | In the given rectangular area Within, clear the pixels on the canvas | |
fillText | Draw the filled text on the canvas | |
drawImage | Draw an image on the canvas | |
fill | None | Fill the current path Fill |
stroke | None | Stroke the current path |
Path | ||
beginPath | None | Start a path |
closePath | None | Close a path |
moveTo | Move the path to the specified point in the canvas , but no lines are created. | |
lineTo | Adds a new point and then creates a line in the canvas from that point to the last specified point. | |
rect | Add a rectangular path to the current path. | |
arc | Add an arc path to the current path, drawn clockwise. | |
quadraticCurveTo | Create a quadratic Bezier curve | |
bezierCurveTo | Create cubic Bezier curve | |
Style | ||
setFillStyle | Set fill style | |
setStrokeStyle | Set line style | |
setShadow | Set shadow | |
setFontSize | Set font Size | |
setLineCap | Set the style of line endpoints | |
setLineJoin | Set the style of the intersection of two lines | |
setLineWidth | Set the line width | |
setMiterLimit | Set the maximum tilt |
Detailed explanation of the method:
scale
After calling the scale() method, the horizontal and vertical coordinates of the path created subsequently will be scaled. If scale is called multiple times, the multiples will be multiplied.
//scale.js <!--scale.wxml --> <canvas canvas-id="1" />
Page({ onReady:function(e){ var context = wx.createContext(); context.rect(5,5,25,15) context.stroke(); context.scale(2,2); //再放大2倍 context.rect(5,5,25,15); context.stroke(); context.scale(2,2); //再放大2倍 context.rect(5,5,25,15) context.stroke(); wx.drawCanvas({ canvasId:1, actions:context.getActions() }); } })
rotate
With the origin as the center, the origin can be modified using the translate method. Rotate the current coordinate axis clockwise. If rotate is called multiple times, the rotation angles will be superimposed.
Parameters | Type | Range | Description |
---|---|---|---|
Number | degrees * Math.PI/180; degrees range is 0~360 | Rotation angle, in radians |
Sample code:
//rotate.js Page({ onReady:function(e){ var context = wx.createContext(); context.rect(50,50,200,200) context.stroke(); context.rotate(5*Math.PI/180) context.rect(50,50,200,200) context.stroke(); context.rotate(5*Math.PI/180) context.rect(50,50,200,200) context.stroke() wx.drawCanvas({ canvasId:1, actions:context.getActions() }); } })
Type | Range | Description | |
---|---|---|---|
Number | Horizontal coordinate translation amount | ||
Number | Vertical coordinate translation amount |
//translate.js
Page({
onReady:function(){
var context = wx.createContext();
context.rect(50,50,200,200)
context.stroke()
context.translate(50,50)
context.rect(50,50,200,200)
context.stroke();
wx.drawCanvas({
canvasId:1,
actions:context.getActions()
});
}
})
Clear the content on the canvas within the rectangular area
Type | Range | Description | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
x coordinate of the upper left corner of the rectangular area | ##y | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
y coordinate of the upper left corner of the rectangular area | width | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The width of the rectangular area | height | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
The height of the rectangular area | Sample code: //clearrect.js Page({ onReady:function(){ var context = wx.createContext(); context.rect(50,50,200,200) context.fill() context.clearRect(100,100,50,50) wx.drawCanvas({ canvasId:1, actions:context.getActions() }); } }) drawImageDraws the image, keeping the original size of the image.
Draw filled text on the canvas.
//filltext.js Page({ onReady:function(){ var context = wx.createContext(); context.setFontSize(14) context.fillText("MINA",50,50) context.moveTo(0,50) context.lineTo(100,50) context.stroke(); context.setFontSize(20) context.fillText("MINA",100,100) context.moveTo(0,100) context.lineTo(200,100) context.stroke(); wx.drawCanvas({ canvasId:1, actions:context.getActions() }); } }) To start creating a path, you need to call fill or stroke to use the path to fill or stroke. Multiple setFillStyle,setStrokeStyle lineTo Adds a new point at the current location, then creates a path in the canvas from that point to the last specified point.
|