Home  >  Article  >  Web Front-end  >  Canvas learning series 1: First introduction to canvas

Canvas learning series 1: First introduction to canvas

巴扎黑
巴扎黑Original
2017-06-23 14:50:411682browse

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>
Or

<script type="text/javascript">var canvas = document.getElementById('canvas');
canvas.width = '600'; //canvas的属性取值为非负整数,所以不能带有pxcanvas.height = '300';</script>

 

 

4. canvas API

canvas元素并未提供很多API,它只提供了两个属性三个方法,而绘图功能的方法与属性全都是canvas的绘图环境(context)对象提供。

  1. width:设置/获取canvas元素绘图表面的宽度,默认值为300。

  2. height:设置/获取canvas元素绘图表面的高度,默认值为150。

  3. getContext(): 返回canvas元素的绘图环境对象。

  4. toDataURL(): 描述:返回一个data URI:会根据type指定的参数形式将canvas中的图片编码成一个UTF-16字符串的形式。

  5. 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn