主頁 > 知識庫 > 基于canvas剪輯區(qū)域功能實現橡皮擦效果

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

熱門標簽:電銷機器人 深圳 外呼系統(tǒng)會封嗎 股票配資電銷機器人 地圖標注如何弄全套標 武漢AI電銷機器人 在電子版地圖標注要收費嗎 萬利達綜合醫(yī)院地圖標注點 實體店地圖標注怎么標 南京電銷外呼系統(tǒng)哪家好

效果如圖

這是基礎結構 沒什么好說的

!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>

下面是重點的js

這里有個坑要十分注意 調用clip()方法的時候,所定義的剪輯區(qū)域總是局限于期初的那個剪輯區(qū)域范圍。
 簡單來說 clip()方法總是在上一次的剪輯區(qū)域基礎上進行操作,所以說我們要把clip()方法放在save()和restore()方法中

var canvas = document.getElementById('canvas'),
context = canvas.getContext('2d'),
strokeStyleSelect = document.getElementById('strokeStyleSelect'),  //畫圖的描邊顏色
fillStyleSelect = document.getElementById('fillStyleSelect'),    //畫圖填充顏色
drawRadio = document.getElementById('drawRadio'),          //畫圖按鈕
eraserRadio = document.getElementById('eraserRadio'),       //橡皮擦按鈕 
eraserShapeSelect = document.getElementById('eraserShapeSelect'), //橡皮擦形狀
eraserWidthSelect = document.getElementById('eraserWidthSelect'), //橡皮擦寬度
ERASER_LINE_WIDTH = 1,
drawingSurfaceImageData,
lastX,
lastY,
mousedown = {},
rubberbandRect = {},
dragging = false
function windowToCanvas(x,y){ //這個函數的作用是捕捉鼠標點在canvas上的坐標
  var bbox=canvas.getBoundingClientRect()
  return {
    x:x-bbox.left,
    y:y-bbox.top
  }
}
function saveDrawingSurface(){  //這個函數的作用是初始化讀取畫布信息并儲存起來
  drawingSurfaceImageData=context.getImageData(0,0,canvas.width,canvas.height)
}
function restoreDrawingSurface(){ //這個函數的作用是讀取畫布信息
  context.putImageData(drawingSurfaceImageData,0,0)
}
function drawGrid(){ //這個函數的作用是填充進橡皮擦的剪輯區(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){ //如果是畫圖狀態(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()
  }
}

總結

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

您可能感興趣的文章:
  • cocos2dx+lua實現橡皮擦功能

標簽:臺州 廣東 濟源 泰安 武威 安徽 濟寧 汕頭

巨人網絡通訊聲明:本文標題《基于canvas剪輯區(qū)域功能實現橡皮擦效果》,本文關鍵詞  基于,canvas,剪輯,區(qū)域,功能,;如發(fā)現本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《基于canvas剪輯區(qū)域功能實現橡皮擦效果》相關的同類信息!
  • 本頁收集關于基于canvas剪輯區(qū)域功能實現橡皮擦效果的相關信息資訊供網民參考!
  • 推薦文章