• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com专业计算机教程网站
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure
您的位置:首页 > 网页设计 >html5 > HTML5 Convas APIs方法详解_html5教程技巧

HTML5 Convas APIs方法详解_html5教程技巧

作者:匿名 字体:[增加 减小] 来源:互联网 时间:2018-12-03

本文主要包含HTML5,Convas,APIs等相关知识,匿名希望在学习及工作中可以帮助到您
☆ canvas.getContext('2d')

不可在convas中直接绘图,必须用该方法获得其二维空间绘图上
下文。

☆ context.beginPath()

表示开始新的路径绘制。

☆ context.isPointInPath(x, y)

判断某个点是否在路径上。在坐标系被转换后该方法不适用。

☆ context.moveTo(x,y)

相当于将画笔从画板提起,笔尖离开画板,然后再将笔尖定位在
(x,y)坐标处,在这个新的位置开始新的绘制。

☆ context.lineTo(x, y)

相当于画笔笔尖不离开画板,画笔笔尖从当前坐标位置移动至
(x,y)坐标处,绘制一条线段。

☆ context.stroke()

在convas上绘图后,一些绘制操作必须调用该方法才能让绘制内
容显示。

☆ context.save()

该方法保存convas的当前状态,无论以后对convas坐任何改变,
只要在做这些改变前保存convas状态,以后就可以调用
context.restore()方法恢复到保存的这个状态。通常在一段新绘制
或修改操作前应该保存convas的原始状态(没有进行任何绘制或更改
),每次在一段新绘制或修改操作结束后在将其恢复到原始状态。这
样有利于以后的绘制操作。
实际上,canvas的2d绘图环境context的许多属性和一些方法与状
态有关,每个属性的值被改变(或者使用某些方法改变绘图状态),
绘图状态就改变。若在每次改变后都保存,则一个属性的多个状态会
以栈(stack)的形式保存,可以依照栈的顺序多次调用restore()方
法来回到相应保存的状态。

☆ context.translate(x, y)

该方法将当前坐标原点移动到(x, y)处。

☆ context.restore()

恢复convas状态为上一次保存的状态。

☆ context.closePath()

This command is very similar in behavior to the lineTo
function, with the difference being that the destination is
automatically assumed to be the
origination of the path. However, the closePath also informs
the canvas that the current shape has closed or formed a
completely contained area. This will be useful for future
fills and strokes.
At this point, you are free to continue with more
segments in your path to create additional subpaths. Or you
can beginPath at any time to start over and clear the path
list entirely.

☆ context.fill();

在设置填充样式后填充闭合路径。调用该方法后不必再调用
context.stroke()方法。

☆ context.fillRect(x, y, width, height)

在(x, y)处绘制并填充宽和长为(width, height)的矩形区域。调
用该方法后不必再调用context.stroke()方法。

☆ context.strokeRect(x, y, width, height)

在(x, y)处绘制宽和长为(width, height)的矩形轮廓。

☆ context.clearRect(x, y, width, height)

清理位置(矩形的左上角)在(x, y,),大小为(width, height)
的矩形区域。
Remove any content from the rectangular area and reset it
to its original, transparent color.
The ability to clear rectangles in the canvas is core to
creating animations and games using the HTML5 Canvas API. By
repeatedly drawing and clearing sections of the canvas, it
is possible to present the illusion of animation, and many
examples of this already exist on the Web. However, to
create animations that perform smoothly, you will need to
utilize clipping features and perhaps even a secondary
buffered canvas to minimize the flickering caused by
frequent canvas clears.

☆ context.drawImage( )

该方法有三个重载,可将图像绘制在canvas上。图像来源可以是
页面中的img标记、JS中的image对象和video的一帧。
•context.drawImage(img, x, y)
在(x, y)处用图像img绘制图像。当canvas的大小大于图像时
,整个图像被绘制;当图像大于canvas时,多余的部分被裁剪。
•context.drawImage(img, x, y, w, h)
在(x, y)处用图像img绘制长和宽为(w, h)的矩形区域。图像
的大小将改变为(w, h)。
•context.drawImage(img, imgx, imgy, imgw, imgh, cx, cy,
cw, ch)
将一个img图像作为绘制对象,裁剪img上位置为(imgx, imgy
)大小为(imgw, imgh)的区域,绘制在canvas内位置为(cx, cy)
处绘制大小为(cw, ch)的区域。
如果图像上裁剪区域超出了图像范围,则会引发异常。
•context.drawImage(video, vx, vy, vw, vh, cx, cy, cw, ch)
将一个video对象作为绘制对象,抓取video上位置为(vx, vy
)大小为(vw, vh)的一帧,在canvas上位置为(cx, cy)处绘制大
小为(cw, ch)的区域。
此外,drawImage()的第一个参数也可以是另一个 canvas。

☆ context.getImageData(x, y, width, height)

该方法从canvas内位置为(x, y)处,获得大小(width, height)
一块像素区域,返回值为一个ImageData对象。ImageData有width,
height和data三个属性。
data属性是一个像素数组,数组中每连续的四个元素代表一个像
素,四个连续元素依次含有RGBA的颜色与透明度信息。四个连续的元
素必须属于一个像素,第一个元素的位置不是随意取的。
像素数组是按照从上到下,从左到右的顺序在canvas中指定区域
扫描获取。像素数组的元素个数为width * height * 4。要获得特定
位置的像素信息。
使用了该方法的Web页面若用浏览器以本地文件方式打开不会正常
工作,通常会产生安全错误(security error)。可以将文件上传至
Web服务器,然后请求访问解决此问题。并且,涉及到的图像,JS和
HTML必须是来自同一个域名。不过,IE9可以通过本地文件访问。
一个例子如下:

var width = imageData.width;
//特定像素在像素区域的位置
var x = 2;
var y = 2;
//绿颜色在像素数组中对应元素的索引
var pixelRedindex = ((y-1)*(width*4))+((x-1)*4);
var pixelGreenindex = pixelRedindex + 1;
var pixelBlueindex = pixelRedindex + 2;
var pixelAlphaindex = pixelRedindex + 3;

var pixel = imageData.data; // CanvasPixelArray

var red = pixel[pixelRedindex];
var green = pixel[pixelGreenindex];
var blue = pixel[pixelBlueindex];
var alpha = pixel[pixelAlphaindex];

☆ context.createImageData(w, h)

产生一个大小为(w, h)的ImageData对象,ImageData对象的意义
同getImageData()所获取的ImageData对象。

☆ context.putImageData(ImageData, x, y, x1, y1, w, h)

将一个ImageData对象绘制到canvas上(x, y)处。后四个参数是可
选参数,用于设定一个裁剪矩形的位置和大小。

☆ context.createLinearGradient(x1, y1, x2, y2)

在(x1, y1)和(x2, y2)之间产生线性渐变。如:

☆ Gradient.addColorStop(offset, color)

用于渐变中,在不同的位置设置渐变颜色。 The color argument
is the color you want to be applied in the stroke or fill at
the offset position. The offset position is a value between
0.0 and 1.0, representing how far along the gradient line
the color should be reached. 如:

gradientName.addColorStop(1, '#552200');

其中color可用CSS中的rgba(r,g,b,a)函数来产生透明渐变,如:

☆ context.createRadialGradient(x0, y0, r0, x1, y1, r1)

在两个圆之间产生放射渐变区域。The first three arguments
represent a circle centered at (x0, y0) with radius r0, and
the last three arguments represent a second circle centered
at (x1, y1) with radius r1. The gradient is drawn across the
area between the two circles.

☆ context.createPattern(img, 'repeatPattern')

用一个图像img产生重复类型为repeatPattern的某路径的
strokeStyle样式或填充的fillStyle样式。repeatPattern的值可以
取repeat、repeat-x、repeat-y和no-repeat之一。如:

参数 img 也可以是另一个 canvas 或 video

☆ context.scale(xMultiple, yMultiple)

两个参数分别指定对象在x和y方向上以后的绘制缩放倍数,大于1
为放大,0和1之间为缩小。若为负值,则可以实现倒影、翻转等效果
。

☆ context.rotate(angle)

用于旋转绘图环境context,以当前坐标原点为按转中心,以弧度
为单位,结合使用Math.PI。参数 angle 为正值时为顺时针旋转,为
负值时按逆时针

您可能想查找下面的文章:

  • HTML5知识点总结
  • HTML5的本地存储
  • HTML5本地存储之IndexedDB
  • Html5实现文件异步上传功能
  • Html5新标签datalist实现输入框与后台数据库数据的动态匹配
  • 详解HTML5 window.postMessage与跨域
  • HTML5拖放API实现拖放排序的实例代码
  • 解决html5中video标签无法播放mp4问题的办法
  • HTML5新特性 多线程(Worker SharedWorker)
  • Html5新增标签有哪些

相关文章

  • 2018-12-03如何通过HTML5触摸事件实现移动端简易进度条
  • 2018-12-03AugularJS基础入门与实践
  • 2018-12-03HTML5 canvas标签实现刮刮卡效果_html5教程技巧
  • 2018-12-03html5 canvas里绘制椭圆并保持线条粗细均匀的技巧_html5教程技巧
  • 2018-12-03用protractor测试canvas绘制(二)
  • 2018-12-03html5大文件上传技术分享
  • 2017-08-06HTML5移动端手机网站开发流程
  • 2018-12-03用HTML5实现桌面提醒功能的一个实例代码
  • 2017-08-06HTML5 贪吃蛇游戏实现思路及源代码
  • 2018-12-03基于SVG和CSS3的可爱卡通小动物动画特效

文章分类

  • html/xhtml
  • html5
  • CSS
  • XML/XSLT
  • Dreamweaver教程
  • Frontpage教程
  • 心得技巧
  • bootstrap
  • vue
  • AngularJS
  • HBuilder教程
  • css3
  • 浏览器兼容
  • div/css
  • 网页编辑器
  • axure

最近更新的内容

    • 用仿ActionScript的语法来编写html5——第九篇,仿URLLoader读取文件
    • H5学习之旅-H5与Php交互(12)
    • HTML5 canvas基本绘图之绘制线条
    • Html5 Canvas初探学习笔记(1)-画一个矩形
    • HTML5 Canvas实现文本对齐的代码总结
    • 混合移动开发框架详解
    • 多年前对 XHTML 和 HTML5 的预测为什么错的这么离谱?
    • fps页游,数据传输技术方案是怎么样的?
    • 如何使用Chrome控制台进行3D模型编辑的实现(代码)
    • iChart-组件定制图形库图表/报表开发教程

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有