HTML5 Canvas图像处理技巧
本文是从 HTML 5 Canvas Tutorial – Displaying Images 这篇文章翻译而来。
Canvas 标记很多年前就被当作一个新的 HTML 标记成员加入到了 HTML5 标准中。在此之前,人们要想实现动态的网页应用,只能借助于第三方的插件,比如 Flash 或 Java,而引入了 Canvas 标记后,人们直接打通了通往神奇的动态应用网页的大门。本教程内容只覆盖了一小部分、但却是非常重要的 canvas 标记的应用功能图像显示和处理。
图像来源
最常见的在 canvas 上画图的方法是使用 Javascript Image 对象。所支持的来源图片格式依赖于浏览器的支持,然而,一些典型的图片格式(png,jpg,gif 等)基本上都没有问题。
图片可以从 DOM 中已经加载的元素中抓取,也可以按需即时创建。
// 抓取页面上已有的图片。 myImage = new Image (); myImage.src = “image.png”;
大多数支持 canvas 标记的浏览器的当前版本中,当图片还没有加载完成时,如果你要去画它,结果是什么事情都不会发生。也就是说,如果你想画一个图片,你需要等它完全加载。你可以使用图片对象的 onload 函数来进行判断。
// Create an image. myImage = new Image (); myImage.onload = function () { // Draw image. myImage.src = “image.png”;
在下面的所有例子中,我们的图片源将会使用这个256×256尺寸的大猩猩。
基本绘画
在最基本的画图操作中,你需要的只是希望图像出现处的位置(x和y坐标)。图像的位置是相对于其左上角来判断的。使用这种方法,图像可以简单的以其原尺寸被画在画布上。
drawImage (image, x, y) var ctx = canvas.getContext (“2d”);ctx.drawImage (myImage, 50, 50); ctx.drawImage (myImage, 125, 125); ctx.drawImage (myImage, 210, 210);
缩放及调整尺寸
改变图像的尺寸,你需要使用重载的 drawImage 函数,提供给它希望的宽度和高度参数。
drawImage (image, x, y, width, border=”1″ Height) var ctx = canvas.getContext (“2d”);ctx.drawImage (myImage, 50, 50, 100, 100); ctx.drawImage (myImage, 125, 125, 200, 50); ctx.drawImage (myImage, 210, 210, 500, 500);
这个例子演示了如何画一个比原图小的图像,一个不同长宽比的图像和一个比原图大的图像的方法。
图像裁剪
最后一个 drawImage 方法的功用是对图像进行裁剪。
drawImage (image, sourceX, sourceY, sourceWidth, sourceborder=”1″ Height, destX, destY, destWidth, destborder=”1″ Height)
参数很多,但基本上你可以把它想成从原图中取出一个矩形区域,然后把它画到画布上目标区域里。
var ctx = canvas.getContext (“2d”);ctx.drawImage (myImage, 0, 0, 50, 50, 25, 25, 100, 100); ctx.drawImage (myImage, 125, 125, 100, 100, 125, 125, 150, 150); ctx.drawImage (myImage, 80, 80, 100, 100, 250, 250, 220, 220);
以上就是HTML5 Canvas图像处理技巧的内容,更多相关内容请关注PHP中文网(www.php.cn)!

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

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

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



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

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

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.

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

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

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

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

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