Get the App
SLTechnology News&Howtos  ›  Development  › 

How to achieve picture scaling by HTML5 Canvas

Shulou Source: shulou.com Published: 2022-06-01 06:56:45 09月26日 Update

Editor to share with you HTML5 Canvas how to achieve picture scaling, I hope you have something to gain after reading this article, let's discuss it together!

Another variation of the drawImage method is the addition of two parameters to control the scaling of the image in canvas.

DrawImage (image, XBI y, width, height)

Where width and height are the size of the image displayed in the canvas, respectively.

Note: the image may become cluttered or blurred due to large zooming. If you have text in your image, it is best not to zoom, as it is likely that the text in the image will become illegible after processing.

Picture zooming example

Description

In this example, I will use a picture to repeat it on the canvas like a background. It is also easy to implement, simply by cycling out the zoomed image. See the code below, the first layer image size is scaled to the original expansion, 50x38 pixels. For the loop is to repeat the row, and the second layer is to repeat the column.

Note: there is still a certain gap between the pictures in this example, and we don't know how to remove them yet.

Code

< html >

< meta http-equiv = " Content-Type" content = " text / html; charset = utf-8" />

Function draw () {

Var ctx = document.getElementById ('canvas'). GetContext (' 2d')

Var img = new Image ()

Img.onload = function () {

For (I = 0; I)

< canvas id = " canvas"宽度= " 400" height = " 300" >

< / /帆布>

After reading this article, I believe you have a certain understanding of "how to achieve picture scaling in HTML5 Canvas". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Tags: Image picture scale code function size width text canvas article height loop two between two example pixel parameter variant large Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Microsoft macOS Huawei vpn