久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

javascript實(shí)現(xiàn)畫板功能

瀏覽:37日期:2023-06-22 08:36:25

本文實(shí)例為大家分享了javascript實(shí)現(xiàn)畫板功能的具體代碼,供大家參考,具體內(nèi)容如下

畫板功能的實(shí)現(xiàn)

<!DOCTYPE html><html> <head> <meta charset='utf-8'> <title></title> <style type='text/css'> *{ 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); } </style> </head> <body> <!-- div.wrapper>canvas+ul.btn-list>li*5>input --> <div class='wrapper'> <canvas height='500'></canvas> <ul class='btn-list'> <li><input type='color' value='colorBoard'></li> <li><input type='button' value='清屏'></li> <li><input type='button' value='橡皮'></li> <li><input type='button' value='撤銷'></li> <li><input type='range' value='線條' min='1' max='30'></li> </ul> </div> </body> <script src='http://m.baoyu77737.com/bcjs/jquery-3.4.1.min.js'></script> <script> 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’; //線條起始與結(jié)尾樣式 this.context.lineJoin = ’round’; //轉(zhuǎn)彎 this.draw(); //畫筆函數(shù) this.btnFn(); //按鈕函數(shù) }, 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, //鼠標(biāo)點(diǎn)距離減去canvas離左邊的距離等于畫布點(diǎn) m_y = e.pageY - c_y; //鼠標(biāo)點(diǎn)距離減去canvas離上邊的距離等于畫布點(diǎn) self.context.beginPath(); self.context.moveTo(m_x,m_y);//鼠標(biāo)在畫布上的點(diǎn) 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){ //定義一把鎖,防止鼠標(biāo)移開滑動(dòng) self.context.lineTo(e.pageX-c_x,e.pageY-c_y); self.context.stroke(); //繪制出路徑 } }) cavs.mouseup(function(){ self.context.closePath(); //結(jié)束自動(dòng)閉合 self.bool = false; //鼠標(biāo)不移動(dòng)時(shí)畫筆斷開 }) cavs.mouseleave(function(){ self.context.closePath(); //結(jié)束自動(dòng)閉合 self.bool = false; //鼠標(biāo)不移動(dòng)時(shí)畫筆斷開 }) }, 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){ //當(dāng)顏色變化時(shí)改變字體的顏色 self.context.strokeStyle = $(this).val(); }) this.lineRuler.change(function(e){ //線條的變化值 self.context.lineWidth = $(this).val(); }) } } drawingLineObj.init(); </script></html>

javascript實(shí)現(xiàn)畫板功能

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: JavaScript
相關(guān)文章:
主站蜘蛛池模板: 轮台县| 子洲县| 阿合奇县| 班戈县| 邵阳市| 习水县| 福贡县| 宣威市| 潮安县| 进贤县| 丽水市| 莱阳市| 获嘉县| 盐山县| 阳春市| 涿鹿县| 石门县| 五峰| 西昌市| 梅河口市| 五原县| 长沙市| 乐都县| 崇文区| 徐汇区| 珠海市| 曲麻莱县| 孝义市| 保康县| 佛冈县| 百色市| 阳东县| 霍邱县| 临安市| 台安县| 屯门区| 桦川县| 奇台县| 安庆市| 元氏县| 天峨县|