canvas 편집 영역 기능 기반 지우개 효과 구현

7285 단어 canvas지우개
효과

이게 기초 구조 라 서 할 말 이 없어 요.

<!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.
여기에 클립()방법 을 사용 할 때 정 의 된 편집 구역 은 항상 초기 편집 구역 범위 에 국한 되 어 있 습 니 다.
 쉽게 말 하면 클립()방법 은 항상 지난번 편집 영역 을 바탕 으로 작 동 하기 때문에 클립()방법 을 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(){ //                   
  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){ //       
      // 
      restoreDrawingSurface()
      drawCri(loc.x,loc.y)
    }else{ //        
      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 편집 구역 기능 을 바탕 으로 지우개 효 과 를 실현 하 는 것 입 니 다.여러분 에 게 도움 이 되 기 를 바 랍 니 다.궁금 한 점 이 있 으 시 면 저 에 게 메 시 지 를 남 겨 주세요.소 편 은 제때에 답 해 드 리 겠 습 니 다!

좋은 웹페이지 즐겨찾기