點評:在html5中我覺得最重要的就是引入了Canvas,使得我們可以在web中繪制各種圖形,而Canvas為基于像素的繪圖。Canvas是一個相當于畫板的html節點,我們必須以js操作繪圖
在html5中我覺得最重要的就是引入了Canvas,使得我們可以在web中繪制各種圖形。給人感覺單在這點上有點模糊我們web和桌面程序的感覺。在html5外web中也有基于xml的繪圖如:VML、SVG。而Canvas為基于像素的繪圖。Canvas是一個相當于畫板的html節點,我們必須以js操作繪圖。復制代碼
代碼如下:
<html>
<head>
</head>
<body>
<canvas>你的瀏覽器還不支持哦</canvas>
<script type="text/javascript">
var width,height,top,left;
width=height=100;
top=left=5;
var x=10;
var y=10;
var c=document.getElementById("myCanvas");
var maxwidth=c.width-5;
var maxheight=c.height-5;
var cxt=c.getContext("2d");
cxt.strokeStyle="#00f";
cxt.strokeRect(0,0,c.width,c.height);
var img=new Image();
img.src="1.gif";
var MyInterval=null;
start();
function Refresh(){
cxt.clearRect(left,top,width,height);
if((left+x)>(maxwidth-width)||left+x<0){
x=-x;
}
if((top+y)>(maxheight-height)||top+y<0){
y=-y;
}
left=left+x;
top=top+y;
cxt.drawImage(img,left,top,width,height);
cxt.font="20pt 宋體";
cxt.fillText("破狼",left,top+25);
}
function start(){
if(MyInterval==null){
MyInterval=setInterval("Refresh()",100);
}
}
function stop(){
if(MyInterval!=null){
clearInterval(MyInterval);
MyInterval=null;
}
}
function InRectangle(x,y,rectx,recty,rwidth,rheight){
return (x>=rectx&&x<=rectx+rwidth)&&(y>=recty&&y<=recty+rheight)
}
c.onmouseover=function(e){
if(InRectangle(e.clientX,e.clientY,left,top,width,height)){
stop();
}
c.onmouseout=function(e){
start();
}
c.onmousemove=function(e){
if(InRectangle(e.clientX,e.clientY,left,top,width,height)){
if(MyInterval!=null){
stop();
}
}else{
start();
}
}
}
</script>
</body>
</html>
新聞熱點
疑難解答