使用HTML5 canvas完成的一个网页白板

该代码能够使用canvas对象完成的一个基本功能。仅供大家参考。

<html>   
   
<head>   
<meta http-equiv="Content-Type" contentType="text/html; charset=UTF-8" %>   
<title>Title</title>   
<script type="text/javascript">   
var ball;    
var mouseX = 100;    
var mouseY = 100;    
var angle = 0;    
var radius = 0;    
   
var ballstyle = "."   
var ballcolor = "#FF0000";    
var allzindex = 0;    
   
var cav_elem;    
var cav_context;    
   
//old positon    
var opos;    
   
function draw(){    
        ball = document.createElement("p");    
        ball.style.position = "absolute";    
        ball.style.color = ballcolor;    
        ball.style.zIndex = allzindex+1;    
        ball.innerHTML = ballstyle;    
        document.body.appendChild(ball);    
        document.all.selected = false;    
        
    ball.style["left"] = mouseX+ "px";    
    ball.style["top"] = mouseY+ "px";    
}    
function draw_cav(npos){    
        
   
         
    if(opos){    
        cav_context.beginPath();    
        // Start from the top-left point.    
        
        cav_context.moveTo(opos.x,opos.y);     
        cav_context.lineTo(npos.x,npos.y);    
        
        cav_context.stroke();    
        cav_context.closePath();    
            
    }    
   
}    
   
function MousePos(e){    
    eee = e || window.event;    
    var x,y;    
    if(!document.all){    
        x = e.pageX;    
        y = e.pageY;    
    }    
    else{    
        x = event.clientX + document.documentElement.scrollLeft;    
        y = event.clientY + document.documentElement.scrollTop;    
    }    
    return {x:x,y:y};    
}    
function setXY(e){    
    eee = e || window.event;    
    var pos = MousePos(e);    
    mouseX = pos.x;    
    mouseY = pos.y;    
    //if(e.button == 1){    
        draw_cav(pos);    
    //}    
    opos = pos;    
}    
window.onload = function(){    
        
        //get canvas and context    
            var elem = document.getElementById('myCanvas');    
            if (elem && elem.getContext) {    
                cav_elem = elem;    
              var context = elem.getContext('2d');    
              if (context) {    
                cav_context = context;    
                //cav_context.fillRect(0, 0, cav_elem.width, cav_elem.height);    
              }    
            }    
                
        //add mouse action    
    document.documentElement.onmousemove = function(e){    
        eee = e || window.event;    
        setXY(e);    
    };    
}    
</script>   
</head>   
<body onselectstart = "return false;">   
    <canvas id="myCanvas" border="1" width="1000" height="400">   
     Fallback content, in case the browser does not support Canvas.    
  </canvas>   
</body>   
</html>   


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值