html实现画板的基本操作,javascript实现画板功能

本文实例为大家分享了javascript实现画板功能的具体代码,供大家参考,具体内容如下

画板功能的实现

*{

margin: 0;

padding: 0;

list-style: none;

}

body{

background:url(11.jpg) 0 0 no-repeat;

}

.wrapper{

margin: 10px;

}

.wrapper canvas{

border: 1px solid blue;

border-radius:25px;

box-shadow: 10px 10px 5px brown;

margin-bottom: 16px;

background-color: #fff;

}

.wrapper .btn-list{

width: 1000px;

text-align: center;

}

.wrapper .btn-list li{

display: inline-block;

margin-left: 40px;

}

.wrapper .btn-list li input{

background-color: darkgreen;

color: blanchedalmond

border: none;

padding: 6px 13px;

cursor: pointer;

border-radius:25px;

font-size: 18px;

display: block;

transition-duration: 0.2s;

}

.wrapper .btn-list li input:hover{

border: 1px solid chocolate;

box-shadow: 0 12px 15px 0 rgba(0,0,0,0.5);

}

var drawingLineObj = {

cavs:$('.cavs'),

context:$('.cavs').get(0).getContext('2d'),

colorBoard:$('#colorBoard'),

cleanBoard:$('#cleanBoard'),

arrImg:[],

eraser:$("#eraser"),

rescind:$('#rescind'),

lineRuler:$('#lineRuler'),

bool:false,

init:function(){

this.context.lineCap = 'round'; //线条起始与结尾样式

this.context.lineJoin = 'round'; //转弯

this.draw(); //画笔函数

this.btnFn(); //按钮函数

},

draw:function(){

var cavs = this.cavs,

self = this;

var c_x = cavs.offset().left, //canvas离左边的距离

c_y = cavs.offset().top; //canvas离上边的距离

cavs.mousedown(function(e){

e = e||window.event;

self.bool = true;

var m_x = e.pageX - c_x, //鼠标点距离减去canvas离左边的距离等于画布点

m_y = e.pageY - c_y; //鼠标点距离减去canvas离上边的距离等于画布点

self.context.beginPath();

self.context.moveTo(m_x,m_y);//鼠标在画布上的点

var imgData = self.context.getImageData(0,0,self.cavs[0].width,self.cavs[0].height);

self.arrImg.push(imgData);

//console.log(self.arrImg);

})

cavs.mousemove(function(e){

if(self.bool){ //定义一把锁,防止鼠标移开滑动

self.context.lineTo(e.pageX-c_x,e.pageY-c_y);

self.context.stroke(); //绘制出路径

}

})

cavs.mouseup(function(){

self.context.closePath(); //结束自动闭合

self.bool = false; //鼠标不移动时画笔断开

})

cavs.mouseleave(function(){

self.context.closePath(); //结束自动闭合

self.bool = false; //鼠标不移动时画笔断开

})

},

btnFn:function(){

var self = this;

$('.btn-list').on('click',function(e){

e = e||window.event;

switch(e.target.id){ //target

case 'cleanBoard':

self.context.clearRect(0,0,self.cavs[0].width,self.cavs[0].height) //[0]

break

case 'eraser':

self.context.strokeStyle = '#fff'

break

case 'rescind':

if(self.arrImg.length>0){

self.context.putImageData(self.arrImg.pop(),0,0);

break

}

}

})

this.colorBoard.change(function(e){ //当颜色变化时改变字体的颜色

self.context.strokeStyle = $(this).val();

})

this.lineRuler.change(function(e){ //线条的变化值

self.context.lineWidth = $(this).val();

})

}

}

drawingLineObj.init();

d300fcbdadbb10adf0fc03f1074ca245.png

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值