html5js做画板背景颜色,js和html5实现画板

html5新添了一个重要又强大的标签元素,该标签真有彻底替换掉flash的尽头,现在很多网页游戏就是用完成的,下面代码就是用该标签制作的一个画板。

效果图:

0818b9ca8b590ca3270a3433284dd417.png

#canvas{cursor:crosshair;}

#red{background:red; width:30px;height: 27px}

#blue{background:blue; width:30px;height: 27px}

#yellow{background:yellow; width:30px;height: 27px}

#white{background:white; width:30px;height: 27px}

#zi{background:#8B026B; width:30px;height: 27px}

画笔颜色:

画笔宽度:

4

8

16

30

function change(){

document.getElementById("image").src=canvas.toDataURL("image/jpg");

//window.open("demo.htm", "height=100px, width=400px");

//alert(document.getElementById("image"));

}

//下拉画笔宽度

window.οnlοad=function(){

var huabi=document.getElementById("sel");

huabi.οnchange=function(){

linw=huabi.value;

};

//linw=huabi;

};

var canvas=document.getElementById("canvas");

var ctx=canvas.getContext("2d");

//画一个黑色矩形

ctx.fillStyle="#002200";

ctx.fillRect(0,0,600,400);

//按下标记

var onoff=false;

var oldx=-10;

var oldy=-10;

//设置颜色默认为白色

var linecolor="white";

//宽度默认为4

var linw=4;

//鼠标移动事件,事件绑定

canvas.addEventListener("mousemove",draw,true);

canvas.addEventListener("mousedown",down,false);

canvas.addEventListener("mouseup",up,false);

function down(event){

onoff=true;

oldx=event.pageX-10;

oldy=event.pageY-10;

}

function up(){

onoff=false;

}

function draw(event){

if(onoff==true)

{

var newx=event.pageX-10;

var newy=event.pageY-10;

ctx.beginPath();

ctx.moveTo(oldx,oldy);

ctx.lineTo(newx,newy);

ctx.strokeStyle=linecolor;

ctx.lineWidth=linw;

ctx.lineCap="round";

ctx.stroke();

oldx=newx;

oldy=newy;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值