canvas学习一之canvas的基本使用
来源:互联网 发布:iphonex预约软件 编辑:程序博客网 时间:2024/06/04 01:21
一、canvas
1.canvas是html5中的一个标签,通过canvas的getContext得到上下文可以绘图等操作。canvas通过js进行API操作,可以得到想要的图形或动画。
2.html5中canvas有两套尺寸,第一个是元素本身的大小,在标签的属性里指定;还有一个是绘图表面的大小,标签属性和css都可以修改。默认情况下,canvas的元素大小和绘图表面都是300*150;当两者不一致时,浏览器会对画图表面进行缩放,使其符合元素的大小。举个例子如下:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>canvas基本使用</title> <style type="text/css"> body { background: #dddddd; } #canvas,#canvas1 { margin: 20px; padding: 20px; background: #ffffff; border: thin inset #aaaaaa; width: 600px; height: 300px; } </style></head><body> <canvas id='canvas'> 不支持canvas </canvas> <canvas id="canvas1" width="600" height="300"> 不支持canvas </canvas></body><script type="text/javascript"> var canvas = document.getElementById('canvas'), context = canvas.getContext('2d'); context.font = '38pt Arial'; context.fillStyle = 'cornflowerblue'; context.strokeStyle = 'blue'; context.fillText("Hello Canvas", canvas.width/2 - 150, canvas.height/2 + 15); context.strokeText("Hello Canvas", canvas.width/2 - 150, canvas.height/2 + 15); var canvas1 = document.getElementById('canvas1'), context1 = canvas1.getContext('2d'); context1.font = '38pt Arial'; context1.fillStyle = 'cornflowerblue'; context1.strokeStyle = 'blue'; context1.fillText("Hello Canvas", canvas1.width/2 - 150, canvas1.height/2 + 15); context1.strokeText("Hello Canvas", canvas1.width/2 - 150, canvas1.height/2 + 15);</script></html>
当没有在标签中指定宽高时,元素本身默认300*150,绘图表面也是300*150,当在css中修改宽高时,只修改元素本身大小,为600*300,绘图表面没有修改,还是300*150,两者不相等,于是浏览器会对画布进行缩放,使之符合大小。这里是横向竖向都放大2倍。
所以,当使用canvas时,最好在标签写好宽高,如果不可以,还可以使用js脚本给canvas加上宽高。
二、2个属性3个方法
2个属性:
width 画图表面的宽;
height 画图表面的高
3个方法:
getContext():获得绘图对象,一般getContext(“2d”)获得2d绘图对象;
toDateURL(type,quality):获得一个数据地址,这个是将canvas画布中的内容转化为图像,再将图像转为base64编码,最后得到的是一串字符串。type有image/png,image/jpeg两种,虽然base64有image/gif,但经我测试用火狐测试,不可以转成gif(?)。quality是图片的质量,从0-1.0的double数值;
toBlob(callback,type,args) Blob二进制大对象,将图片转成Blob.callback一个以获得的blob为参数的回调函数,type是图片类型,args图片质量;
将上面代码,修改一下,得到两个方法的照片:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>两种方法,将canvas转成图像</title> <style type="text/css"> body { background: #dddddd; } #canvas{ margin: 20px; padding: 20px; background: #ffffff; border: thin inset #aaaaaa; width: 600px; height: 300px; } </style></head><body> <canvas id='canvas' width="600" height="300"> 不支持canvas </canvas> <img id="img"></body><script type="text/javascript"> var canvas = document.getElementById('canvas'), context = canvas.getContext('2d'); context.font = '38pt Arial'; context.fillStyle = 'cornflowerblue'; context.strokeStyle = 'blue'; context.fillText("Hello Canvas", canvas.width/2 - 150, canvas.height/2 + 15); context.strokeText("Hello Canvas", canvas.width/2 - 150, canvas.height/2 + 15); var href = canvas.toDataURL("image/png", 0.1); var image = document.getElementById("img");//静态获取img节点 image.src=href; canvas.toBlob(function(blob){ var newImg = document.createElement("img"),//动态生成img节点 url = URL.createObjectURL(blob); newImg.onload = function() { URL.revokeObjectURL(url);//加载完成,释放url }; newImg.src = url; document.body.appendChild(newImg); },"image/png",0.1)</script></html>
两者图像地址不一致:
toDataURL得到data:image/png;base64开头的字符串
toBlob得到blob:null/d871fc67-7a9a-41e1-82b2-9954eca7d198
toDataURL得到图像可以另存为保存到硬盘,但是blob不可以。
- canvas学习一之canvas的基本使用
- canvas的基本使用
- Canvas的基本使用
- canvas学习之图像的基本操作
- HTML5学习(2) canvas的使用一
- Canvas学习之基础一
- android canvas的基本使用
- android Canvas的基本使用
- Canvas绘图的使用(一)
- 学习:Canvas基本用法
- Canvas基本使用
- HTML5>canvas基本使用
- Android Canvas 基本使用
- Canvas基本使用
- Canvas教程 Canvas tutorial之基本用法
- JavaScript之使用Canvas绘图(一)
- (十)Canvas 的基本使用
- Canvas的基本介绍和使用
- Fork/Join 框架
- YOLO V2 的mAP数据测试
- c++派生类向基类转换的可访问性
- 二叉树的递归算法应用
- TCP流量控制和拥塞控制
- canvas学习一之canvas的基本使用
- 严蔚敏版数据结构课本代码——非降序线性表(BUCT-JK1602-LLP)
- 正则表达式
- Python源码剖析(02 Python中的整数对象)
- python 如何进行多个字符串的拼接
- defer和async定义及区别
- 栅栏密码解码python实现
- 645. Set Mismatch(数学)
- 【笔试卷】WANGLONG