Canvas learning series 1: First introduction to canvas
You have recently started to learn canvas, and you plan to record some notes and summaries of the entire learning process of canvas. If there are any deficiencies, please point them out.
##1. Canvas introduction
The appearance of the Canvas element can be said to open the Web World drawing animation, the door to graphics, its function is very powerful
The canvas element is the most powerful element in HTML5, and its capabilities are mainly through the Context (drawing context/drawing environment) in Canvas displayed by the object. This object is obtained from the canvas itself.
var canvas = getElementById('canvas');var context = canvas.getContext('2d');
2. Backup content of canvas
Canvas The text contained between elements, this text is called "backup content", which will only be displayed when the browser does not support the canvas element
<canvas>当前浏览器不支持canvas元素,请更换浏览器</canvas>
3. Canvas size
The default width of the canvas element is 300px and the height is 150px.
We can modify the size of the canvas through the width and height attributes of the canvas, and we can also modify the size of the canvas element through CSS. But there is a difference between the two modifications.
Canvas actually has two sets of sizes:
One is the size of the canvas element, and the other is the size of the canvas drawing surface.
When we use the canvas properties width and height, we actually modify the size of the element and the size of the drawing surface at the same time
When we When setting with CSS, the size of the canvas element will be modified, which will not affect the size of the drawing surface. At this time, the browser will scale the drawing surface, which will produce effects we do not want to get
The performance when the width and height attributes modify the canvas size
<canvas id="canvas" width="600" height="300">当前浏览器不支持canvas,请更换浏览器</canvas> <script type="text/javascript">var canvas = document.getElementById('canvas');var cxt = canvas.getContext('2d'); cxt.font = "38px Arial"; cxt.fillStyle = "#427ACC"; cxt.strokeStyle = "#00116A"; cxt.fillText('Hello Canvas', canvas.width/2 - 110, canvas.height/2 + 15); cxt.strokeText('Hello Canvas', canvas.width/2 - 110, canvas.height/2 + 15);</script>
Performance when using CSS to modify the size of canvas elements
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>canvas尺寸问题</title> <style>#canvas { margin: 0 auto; padding: 0; width: 600px; height: 300px; border: 1px solid #ccc; }</style> </head> <body> <img src="" alt="" id="dataImage"> <canvas id="canvas">当前浏览器不支持canvas,请更换浏览器</canvas> <script type="text/javascript">var canvas = document.getElementById('canvas');var cxt = canvas.getContext('2d'); cxt.font = "38px Arial"; cxt.fillStyle = "#427ACC"; cxt.strokeStyle = "#00116A"; cxt.fillText('Hello World', canvas.width/2 - 110, canvas.height/2 + 15); cxt.strokeText('Hello World', canvas.width/2 - 110, canvas.height/2 + 15);</script> </body> </html>
So when we set the size of the Canvas element, it is best not to use CSS to set it. We can set it like this
<canvas id="canvas" width="600" height="300">当前浏览器不支持canvas,请更换浏览器</canvas>
<script type="text/javascript">var canvas = document.getElementById('canvas'); canvas.width = '600'; //canvas的属性取值为非负整数,所以不能带有pxcanvas.height = '300';</script>
4. canvas API
canvas元素并未提供很多API,它只提供了两个属性三个方法,而绘图功能的方法与属性全都是canvas的绘图环境(context)对象提供。
width:设置/获取canvas元素绘图表面的宽度,默认值为300。
height:设置/获取canvas元素绘图表面的高度,默认值为150。
getContext(): 返回canvas元素的绘图环境对象。
toDataURL(): 描述:返回一个data URI:会根据type指定的参数形式将canvas中的图片编码成一个UTF-16字符串的形式。
toBold(): 描述:创建Blob对象,用以展示canvas上的图片;这个图片文件可以被缓存或保存到本地,由User Agent( 用户代理端 )自行决定。
toDataURL():
type 可选参数
图片格式,默认为 image/png
encoderOptions 可选参数
当图片格式为 image/jpeg 或 image/webp的情况下,可以从 0 到 1 的区间内选择图片的质量。
如果超出取值范围,将会使用默认值 0.92,默认分辨率为96dpi。
这里值得注意:
如果canvas的高度或者宽度为0时,会返回字符串 "data:,"
如果传入的类型不是 "image/png", 但是返回的值以 "data: image/png"开头,说明传入的类型不支持
Chrome支持“image/webp”类型
尽管在默认情况下canvas对象是一副位图,但是并不是HTML中的img元素,所以我们可以利用toDataURL方法创建一幅表示canvas的图像;也可以利用此方法创建和操作缓冲canvas。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>canvas尺寸问题</title> <style>#canvas { margin: 0 auto; padding: 0; display: none; }</style> </head> <body> <img src="" alt="" id="dataImage"> <canvas id="canvas">当前浏览器不支持canvas,请更换浏览器</canvas> <script type="text/javascript">var canvas = document.getElementById('canvas');var dataImage = document.getElementById('dataImage'); canvas.width = '600'; //canvas的属性取值为非负整数,所以不能带有pxcanvas.height = '300';var cxt = canvas.getContext('2d'); cxt.font = "38px Arial"; cxt.fillStyle = "#427ACC"; cxt.strokeStyle = "#00116A"; cxt.fillText('Hello World', canvas.width/2 - 110, canvas.height/2 + 15); cxt.strokeText('Hello World', canvas.width/2 - 110, canvas.height/2 + 15);var dataUrl = canvas.toDataURL(); dataImage.src = dataUrl;</script> </body> </html>
toBold():
目前该方法只有Firefox与IE10浏览器支持
参考文章:
MDN Web 技术文档
The above is the detailed content of Canvas learning series 1: First introduction to canvas. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The Xiaomi Mi 15 series is expected to be officially released in October, and its full series codenames have been exposed in the foreign media MiCode code base. Among them, the flagship Xiaomi Mi 15 Ultra is codenamed "Xuanyuan" (meaning "Xuanyuan"). This name comes from the Yellow Emperor in Chinese mythology, which symbolizes nobility. Xiaomi 15 is codenamed "Dada", while Xiaomi 15Pro is named "Haotian" (meaning "Haotian"). The internal code name of Xiaomi Mi 15S Pro is "dijun", which alludes to Emperor Jun, the creator god of "The Classic of Mountains and Seas". Xiaomi 15Ultra series covers

The Charm of Learning C Language: Unlocking the Potential of Programmers With the continuous development of technology, computer programming has become a field that has attracted much attention. Among many programming languages, C language has always been loved by programmers. Its simplicity, efficiency and wide application make learning C language the first step for many people to enter the field of programming. This article will discuss the charm of learning C language and how to unlock the potential of programmers by learning C language. First of all, the charm of learning C language lies in its simplicity. Compared with other programming languages, C language

Learn Pygame from scratch: complete installation and configuration tutorial, specific code examples required Introduction: Pygame is an open source game development library developed using the Python programming language. It provides a wealth of functions and tools, allowing developers to easily create a variety of type of game. This article will help you learn Pygame from scratch, and provide a complete installation and configuration tutorial, as well as specific code examples to get you started quickly. Part One: Installing Python and Pygame First, make sure you have

Since the Huawei Mate60 series went on sale last year, I personally have been using the Mate60Pro as my main phone. In nearly a year, Huawei Mate60Pro has undergone multiple OTA upgrades, and the overall experience has been significantly improved, giving people a feeling of being constantly new. For example, recently, the Huawei Mate60 series has once again received a major upgrade in imaging capabilities. The first is the new AI elimination function, which can intelligently eliminate passers-by and debris and automatically fill in the blank areas; secondly, the color accuracy and telephoto clarity of the main camera have been significantly upgraded. Considering that it is the back-to-school season, Huawei Mate60 series has also launched an autumn promotion: you can enjoy a discount of up to 800 yuan when purchasing the phone, and the starting price is as low as 4,999 yuan. Commonly used and often new products with great value

When editing text content in Word, you sometimes need to enter formula symbols. Some guys don’t know how to input the root number in Word, so Xiaomian asked me to share with my friends a tutorial on how to input the root number in Word. Hope it helps my friends. First, open the Word software on your computer, then open the file you want to edit, and move the cursor to the location where you need to insert the root sign, refer to the picture example below. 2. Select [Insert], and then select [Formula] in the symbol. As shown in the red circle in the picture below: 3. Then select [Insert New Formula] below. As shown in the red circle in the picture below: 4. Select [Radical Formula], and then select the appropriate root sign. As shown in the red circle in the picture below:

Title: Learn the main function in Go language from scratch. As a simple and efficient programming language, Go language is favored by developers. In the Go language, the main function is an entry function, and every Go program must contain the main function as the entry point of the program. This article will introduce how to learn the main function in Go language from scratch and provide specific code examples. 1. First, we need to install the Go language development environment. You can go to the official website (https://golang.org

Explore the Canvas framework: To understand what are the commonly used Canvas frameworks, specific code examples are required. Introduction: Canvas is a drawing API provided in HTML5, through which we can achieve rich graphics and animation effects. In order to improve the efficiency and convenience of drawing, many developers have developed different Canvas frameworks. This article will introduce some commonly used Canvas frameworks and provide specific code examples to help readers gain a deeper understanding of how to use these frameworks. 1. EaselJS framework Ea

Understand the power and application of canvas in game development Overview: With the rapid development of Internet technology, web games are becoming more and more popular among players. As an important part of web game development, canvas technology has gradually emerged in game development, showing its powerful power and application. This article will introduce the potential of canvas in game development and demonstrate its application through specific code examples. 1. Introduction to canvas technology Canvas is a new element in HTML5, which allows us to use
