Get the App
SLTechnology News&Howtos  ›  Development  › 

How to draw in & lt;canvas/>

Shulou Source: shulou.com Published: 2022-06-01 06:57:24 09月30日 Update

This article mainly explains "how to draw in the middle". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to draw in the middle".

Draw a picture on Canvas

All drawings in must be done in JavaScript:

WXML: (we will use this WXML as a template in the following example if there is no special declaration, so we will not repeat it.)

JS: (we will put JS in onLoad in the next example)

Const ctx = wx.createCanvasContext ('myCanvas') ctx.setFillStyle (' red') ctx.fillRect (10,10,150,75) ctx.draw () first step: create a Canvas drawing context

First, we need to create a Canvas drawing context CanvasContext.

CanvasContext is an object built into Mini Program, and there are some drawing methods:

Const ctx = wx.createCanvasContext ('myCanvas') step 2: describe the drawing using the Canvas drawing context

Next, let's describe what to draw in Canvas.

Set the fill color of the drawing context to red:

Ctx.setFillStyle ('red')

Use the fillRect (x, y, width, height) method to draw a rectangle and fill it with the red you just set:

Ctx.fillRect (10,10,150,75) step 3: draw

Tell the component that you want to draw the description you just gave:

Ctx.draw () Thank you for your reading, the above is the content of "how to draw in". After the study of this article, I believe you have a deeper understanding of the problem of how to draw in, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Drawing up and down context content learning next examples methods red special objects that is ideas situations articles more templates knowledge knowledge points rectangle Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB NVidia Redmi Shulou Tech Info OPPO Reno