HTML5(五) Picture and Text

本文介绍HTML5 Canvas API的基本用法,包括图片对象的创建、绘制、缩放、裁剪及平铺显示,以及文本样式的设置。通过实例演示如何实现图片的动态展示和文本的精确控制。
HTML5(五) Picture and Text

1. image object
Image is for a picture, we have many ways to get a image object.
a. document.images array
b. document.getElementsByTagName or document.getElementById
c. create a new Image object, and set a picture URL
var img = new Image();
img.src = 'my_image.png';
d. data:url
encode all the picture data via BASE64 to /9j/4QDfRXhpZgAASUkqAAgAAAAFABIBAwABAAAAAQAAADEBAgAVAAAASgAAADIBAgAUAAAAXw
and put them all in the html

2. drawing object
drawImage(image,x,y)
(x,y) is the left top point of the picture, it decides the postion

my example of the small picture, test5.html:
<canvas id="canvas1" width="250" height="300" style="background-color:black">
you are out!
</canvas><br/>
<input type="button" value="show the girl" onclick="Show();" />
<input type="button" value="hide" onclick="Clear();" />

<script type="text/javascript">
//picture data Base64 encode
IMG_SRC='data:image/gif;base64,/9j/4QDfRXhpZgAASUkqAAgAAAA......'//省略40字节
//small
IMG_SRC_SMALL='data:image/gif;base64,/9j/4QDfRXhpZgAASUkqAAgAAAAFABIBAwABAAAAAQAAADEBAgAVAAAASgAAADIBAgAUAAAAXwAAABMCAwABAAAAAQAAAGmHBAABAAAAcwAAAAAAAABBQ0QgU3lzdGVtcyDK/cLrs8nP8QAyMDEwOjEwOjA0IDE1OjM5OjU3AAUAAJAHAAQAAAAwMjIwkJICAAQAAAAyODEAAqAEAAEAAAAXAAAAA6AEAAEAAAAeAAAABaAEAAEAAAC1AAAAAAAAAAIAAQACAAQAAABSOTgAAgAHAAQAAAAwMTAwAAAAAAAAAAD//gAqSW50ZWwoUikgSlBFRyBMaWJyYXJ5LCB2ZXJzaW9uIDEsNSw0LDM2AP/AABEIAB4AFwMBIQACEQEDEQH/2wCEAAMCAgICAQMCAgIDAwMDBAcEBAQEBAkGBgUHCgkLCwoJCgoMDREODAwQDAoKDxQPEBESExMTCw4VFhUSFhESExIBBAUFBgUGDQcHDRsSDxIbGx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ElementById("canvas1").getContext("2d");
img=new Image();
img.onload=function(){
ctx.drawImage(img,0,0);
}
img.src=IMG_SRC_SMALL;
}

function Clear(){
ctx = document.getElementById("canvas1").getContext("2d");
ctx.clearRect(0,0,250,300);
}
</script>

3. enlarge and make it small
drawImage(image,x,y,width,height)

my test page is test5-1.html:
<canvas id="canvas2" width="250" height="300" style="background-color:black">
you are out
</canvas><br/>
horizontal<input type="range" min="1" max="200" onchange="Scale1(event)"/><br/>
vertical<input type="range" min="1" max="200" onchange="Scale2(event)"/><br/>
percent<input type="range" min="1" max="200" onchange="Scale3(event)"/><br/>
tile<input type="range" min="1" max="100" value="1" onchange="Scale4(event)"/><br/>

<script type="text/javascript">

function Scale1(){
//caculate
var scale=event.target.value/10
ctx = document.getElementById("canvas2").getContext("2d");
ctx.clearRect(0,0,250,300);
img=new Image();
img.onload=function(){
ctx.drawImage(img,0,0,img.width*scale,img.height);
}
img.src=IMG_SRC_SMALL;
}

function Scale2(){
var scale=event.target.value/10
ctx = document.getElementById("canvas2").getContext("2d");
ctx.clearRect(0,0,250,300);
img=new Image();
img.onload=function(){
ctx.drawImage(img,0,0,img.width,img.height*scale);
}
img.src=IMG_SRC_SMALL;
}

function Scale3(){
var scale=event.target.value/10
ctx = document.getElementById("canvas2").getContext("2d");
ctx.clearRect(0,0,250,300);
img=new Image();
img.onload=function(){
ctx.drawImage(img,0,0,img.width*scale,img.height*scale);
}
img.src=IMG_SRC_SMALL;
}

function Scale4(){
var scale=event.target.value;
ctx = document.getElementById("canvas2").getContext("2d");
ctx.clearRect(0,0,250,300);
img=new Image();
img.onload=function(){
var n1=img.width/scale;
var n2=img.height/scale;

for(var i=0;i<n1;i++)
for(var j=0;j<n2;j++)
ctx.drawImage(img,i*img.width/scale,j*img.height/scale,img.width/scale,img.height/scale);
}
img.src=IMG_SRC_SMALL;
}
</script>

4. cut the image
drawImage(image,sx,sy,sWidth,sHeight,dx,dy,dWidth,dHeight)
(sx,sy) the start point of the cut picture
sWidth and sHeight are the width and height of the cut picture.

(dx,dy) the start point of the output picture
dWidth and dHeight are the width and height of the output picture.

5. Image
createPattern(image,type)
type: repeat, repeat-x, repeat-y, no-repeat
my example file is test5-2.html:
<canvas id="canvas3" width="250" height="300" style="background-color:black">
you are out!
</canvas><br/>
<input type="button" value="Go" onclick="Patterns();" />
<input type="button" value="Clear" onclick="ClearPatterns();" />

<script type="text/javascript">
function Patterns(){
ctx = document.getElementById("canvas3").getContext("2d");
img=new Image();
img.src=IMG_SRC_SMALL;
img.onload=function(){
var ptrn = ctx.createPattern(img,'repeat');
ctx.fillStyle = ptrn;
ctx.fillRect(0,0,250,300);
}
}

function ClearPatterns(){
ctx = document.getElementById("canvas3").getContext("2d");
ctx.clearRect(0,0,250,300);
}
</script>

6.Text
font: text style, it is like font-family in CSS
textAlign: start, end, left, right, center, default value is start.
textBaseline: top, hanging, middle, alphabetic, ideographic, bottom. default value is alphabetic.

fillText
strokeText

my example file is test5-3.html:
<canvas id="canvas3" width="250" height="300" style="background-color:black">
you are out!
</canvas><br/>
<input type="button" value="Show" onclick="Show();" />
<input type="button" value="Clear" onclick="ClearPatterns();" />

<script type="text/javascript">

function Show(){
ctx = document.getElementById("canvas3").getContext("2d");
ctx.fillStyle = 'white';
ctx.font = 'italic 30px sans-serif';
ctx.textBaseline = 'top';
ctx.fillText('Hello world1!', 0, 0);
ctx.font = 'italic 30px sans-serif';
ctx.fillText('Hello world2!', 0, 50);
}

function ClearPatterns(){
ctx = document.getElementById("canvas3").getContext("2d");
ctx.clearRect(0,0,250,300);
}
</script>

references:
http://www.blogjava.net/myqiao/category/46360.html
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值