国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 編程設(shè)計(jì) > 正文

基于canvas剪輯區(qū)域功能實(shí)現(xiàn)橡皮擦效果

2020-01-31 16:12:58
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

效果如圖

這是基礎(chǔ)結(jié)構(gòu) 沒(méi)什么好說(shuō)的

<!DOCTYPE html><html lang="en"><head>  <meta charset="UTF-8">  <meta name="viewport" content="width=device-width, initial-scale=1.0">  <meta http-equiv="X-UA-Compatible" content="ie=edge">  <title>Document</title>  <style>  *{padding: 0;margin: 0}  a{text-decoration: none}  img{border: none}  ul,ol{list-style: none}  br{font-size: 0;line-height: 0;font-size: 0}  canvas{border: 1px solid red;background: white}  body{background: gray;text-align: center}  </style></head><body>    <div id='controls'>        Stroke color: <select id='strokeStyleSelect'>                 <option value='red'>red</option>                 <option value='green'>green</option>                 <option value='blue'>blue</option>                 <option value='orange'>orange</option>                 <option value='cornflowerblue'>cornflowerblue</option>                 <option value='goldenrod'>goldenrod</option>                 <option value='navy' selected>navy</option>                 <option value='purple'>purple</option>                 <option value='purple'>purple</option>               </select>        Fill color: <select id='fillStyleSelect'>                 <option value='rgba(255,0,0,0.5)'>semi-transparent red</option>                 <option value='green'>green</option>                 <option value='rgba(0,0,255,0.5)'>semi-transparent blue</option>                 <option value='orange'>orange</option>                 <option value='rgba(100,140,230,0.5)'>semi-transparent cornflowerblue</option>                 <option value='goldenrod' selected>goldenrod</option>                 <option value='navy'>navy</option>                 <option value='purple'>purple</option>               </select>        Draw <input id='drawRadio' name='drawEraserRadios' type='radio' checked/>        Erase <input id='eraserRadio' name='drawEraserRadios' type='radio'/>        Eraser: <select id='eraserShapeSelect'>                <option value='circle'>circle</option>                <option value='square'>square</option>               </select>        Eraser width: <select id='eraserWidthSelect'>                <option value=25>25</option>                <option value=50>50</option>                <option value=75>75</option>                <option value=100>100</option>                <option value=125>125</option>                <option value=150>150</option>                <option value=175>175</option>                <option value=200>200</option>               </select>       </div>  <canvas id="canvas" width="950" height="600"></canvas></body></html><script src="./js/test9.js"></script>

下面是重點(diǎn)的js

這里有個(gè)坑要十分注意 調(diào)用clip()方法的時(shí)候,所定義的剪輯區(qū)域總是局限于期初的那個(gè)剪輯區(qū)域范圍。
 簡(jiǎn)單來(lái)說(shuō) clip()方法總是在上一次的剪輯區(qū)域基礎(chǔ)上進(jìn)行操作,所以說(shuō)我們要把clip()方法放在save()和restore()方法中

var canvas = document.getElementById('canvas'),context = canvas.getContext('2d'),strokeStyleSelect = document.getElementById('strokeStyleSelect'),  //畫(huà)圖的描邊顏色fillStyleSelect = document.getElementById('fillStyleSelect'),    //畫(huà)圖填充顏色drawRadio = document.getElementById('drawRadio'),          //畫(huà)圖按鈕eraserRadio = document.getElementById('eraserRadio'),       //橡皮擦按鈕 eraserShapeSelect = document.getElementById('eraserShapeSelect'), //橡皮擦形狀eraserWidthSelect = document.getElementById('eraserWidthSelect'), //橡皮擦寬度ERASER_LINE_WIDTH = 1,drawingSurfaceImageData,lastX,lastY,mousedown = {},rubberbandRect = {},dragging = falsefunction windowToCanvas(x,y){ //這個(gè)函數(shù)的作用是捕捉鼠標(biāo)點(diǎn)在canvas上的坐標(biāo)  var bbox=canvas.getBoundingClientRect()  return {    x:x-bbox.left,    y:y-bbox.top  }}function saveDrawingSurface(){  //這個(gè)函數(shù)的作用是初始化讀取畫(huà)布信息并儲(chǔ)存起來(lái)  drawingSurfaceImageData=context.getImageData(0,0,canvas.width,canvas.height)}function restoreDrawingSurface(){ //這個(gè)函數(shù)的作用是讀取畫(huà)布信息  context.putImageData(drawingSurfaceImageData,0,0)}function drawGrid(){ //這個(gè)函數(shù)的作用是填充進(jìn)橡皮擦的剪輯區(qū)域  context.save()  context.fillStyle="#fff"  context.fillRect(0,0,canvas.width,canvas.height)  context.restore()}function drawrubber(x,y){  context.beginPath()  context.arc(x,y,eraserWidthSelect.value,0,Math.PI*2,false)  context.clip()}function drawCri(x,y){  var x_width=Math.abs(x-mousedown.x)  var y_width=Math.abs(y-mousedown.y)  var radius=Math.sqrt(x_width*x_width+y_width*y_width) context.save()  context.beginPath()  context.fillStyle=fillStyleSelect.value;  context.arc(mousedown.x,mousedown.y,radius,0,Math.PI*2,false)  context.fill() context.restore()}canvas.onmousedown=function(e){  var loc=windowToCanvas(e.clientX,e.clientY)  mousedown.x=loc.x  mousedown.y=loc.y  lastX=loc.x  lastY=loc.y  saveDrawingSurface()  dragging=true}canvas.onmousemove=function(e){  if(dragging){    var loc=windowToCanvas(e.clientX,e.clientY)    if(drawRadio.checked){ //如果是畫(huà)圖狀態(tài)      //       restoreDrawingSurface()      drawCri(loc.x,loc.y)    }else{ //如果是橡皮擦狀態(tài)      context.save()      drawrubber(loc.x,loc.y)      drawGrid()      context.restore()    }  }}canvas.onmouseup=function(e){  dragging=false;  var loc=windowToCanvas(e.clientX,e.clientY)  if(drawRadio.checked){  lastX=loc.x;  lastY=loc.y;  restoreDrawingSurface()  drawCri(lastX,lastY)  }else{  context.save()  drawrubber(loc.x,loc.y)  drawGrid()  context.restore()  }}

總結(jié)

以上所述是小編給大家介紹的基于canvas剪輯區(qū)域功能實(shí)現(xiàn)橡皮擦效果,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 蒙阴县| 邛崃市| 湟源县| 民乐县| 都江堰市| 沧州市| 华坪县| 宁海县| 泰安市| 莱州市| 扶沟县| 乐至县| 敦煌市| 彩票| 牟定县| 辽宁省| 永吉县| 康马县| 华安县| 教育| 内乡县| 拜城县| 阜新市| 亚东县| 错那县| 固原市| 荃湾区| 成安县| 固安县| 本溪市| 宝山区| 阜宁县| 滦平县| 三门峡市| 汶川县| 漾濞| 天气| 进贤县| 淮阳县| 新丰县| 扶余县|