欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2020年04月12日 11:17:08   作者:liu126120  
這篇文章主要為大家詳細介紹了javascript實現(xiàn)畫板功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

畫板功能的實現(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 class="cavs" width="1000" height="500"></canvas>
 <ul class="btn-list">
 <li><input type="color" id="colorBoard" value="colorBoard"></li>
 <li><input type="button" id="cleanBoard" value="清屏"></li>
 <li><input type="button" id="eraser" value="橡皮"></li>
 <li><input type="button" id="rescind" value="撤銷"></li>
 <li><input type="range" id="lineRuler" value="線條" min="1" max="30"></li>
 </ul>
 </div>
 </body>
 
 <script src="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, //鼠標點距離減去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(); //結(jié)束自動閉合
 self.bool = false; //鼠標不移動時畫筆斷開
 
 })
 cavs.mouseleave(function(){
 self.context.closePath(); //結(jié)束自動閉合
 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){ //當(dāng)顏色變化時改變字體的顏色
 self.context.strokeStyle = $(this).val();
 })
 
 this.lineRuler.change(function(e){ //線條的變化值
 self.context.lineWidth = $(this).val();
 })
 
 }
 
 }   
 drawingLineObj.init();
 
 </script>
</html>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論