Home Web Front-end H5 Tutorial HTML5 CANVAS:绘制图片

HTML5 CANVAS:绘制图片

May 24, 2018 pm 01:50 PM
canvas html5

HTML5 CANVAS:绘制图片

通过前面的学习,我们现在已经可以在HTML5 canvas中绘制图形和文字,并给它们设置一些样式。我们还可以在中绘制图片。用于在作为绘制源的图片可以是下面的几种元素类型:

HTMLImageElement:可以是由Image()构造函数创建的图片,也可以是任何的元素。

HTMLVideoElement:使用一个HTML

HTMLCanvasElement:也可以使用另一个元素作为图片源。

  绘制图片
  我们可以通过2D上下文的三种方法来在

  •   drawImage(image, dx, dy);

  •   drawImage(image, dx, dy, dw, dh);

  •   drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dw, dh);


  基本绘制图片方法:drawImage(image, dx, dy)

  这个方法是在

  下面是一个例子。这个例子在

var ctx = document.getElementById('ex1').getContext('2d');
var img = new Image();
img.onload = function(){
    ctx.drawImage(img,0,0);
};
img.src = 'img/canvas-image-1.jpg';
Copy after login
      上面的代码的返回结果如下:

962.png

  绘制并缩放图片:drawImage(image, dx, dy, dw, dh)
  第二种在
  下面的例子中,我们将绘制的图片缩小1/3左右,然后将它重复排列形成一个网格。

var ctx = document.getElementById('ex2').getContext('2d');
var img = new Image();
img.onload = function(){
    for (var i=0;i<4;i++){
      for (var j=0;j<5;j++){
        ctx.drawImage(img,j*60,i*60,60,60);
      }
    }
};
img.src = &#39;img/canvas-image-2.jpg&#39;;
Copy after login

  上面的代码的返回结果如下:

963.png


  图片切片方法:drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dw, dh)
  第三种在中绘制图片的方法有8个参数。image是源图片,sx和sy是“sourceX”和“sourceY”的简写,这两个参数决定从什么位置开始在源图片上裁剪出一个矩形区域,这个区域的图片将会被绘制在Canvas中。sWidth和sHeight表示矩形区域的宽度和高度。剩下的4个参数和上面的绘制图片方法中的描述相同。看下面的图片,图片上标出了各个参数的位置。

964.jpg

  来看下面的一个例子。这个例子中,我们将源图片剪裁出一部分,然后将它绘制在canvas的一个边框图片之上。

var canvas = document.getElementById(&#39;ex3&#39;);
var ctx = canvas.getContext(&#39;2d&#39;);
// 绘制图片切片
ctx.drawImage(document.getElementById(&#39;source&#39;),
            98, 205, 104, 124, 21, 20, 87, 104);
// 绘制边框图片
ctx.drawImage(document.getElementById(&#39;frame&#39;),0,0);
Copy after login
      上面的代码得到的结果如下:

965.png

  创建和调用图片
  在你能够在Canvas中绘制图片之前,你需要创建一个Image对象,然后将图片加载到内存中。下面是完成这个操作的js代码:

var image = new Image();
image.src = "img/sample.png";
Copy after login

  在你能够绘制图片之前,图片必须被完全加载。为了确保图片被完全加载,你可以为图片添加一个事件监听,这个事件监听中的方法会在图片被完全加载之后被调用。下面是一个示例代码:

image.addEventListener(&#39;load&#39;, drawImage1);
Copy after login
      或者:
var img = new Image();
img.onload = function(){
    ctx.drawImage(img,0,0);
};
Copy after login

以上就是HTML5 CANVAS:绘制图片的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles