濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > 基于canvas剪輯區(qū)域功能實(shí)現(xiàn)橡皮擦效果

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

熱門標(biāo)簽:電銷機(jī)器人 深圳 外呼系統(tǒng)會(huì)封嗎 股票配資電銷機(jī)器人 地圖標(biāo)注如何弄全套標(biāo) 武漢AI電銷機(jī)器人 在電子版地圖標(biāo)注要收費(fèi)嗎 萬(wàn)利達(dá)綜合醫(yī)院地圖標(biāo)注點(diǎn) 實(shí)體店地圖標(biāo)注怎么標(biāo) 南京電銷外呼系統(tǒ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 = false
function 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ù)大家的!

您可能感興趣的文章:
  • cocos2dx+lua實(shí)現(xiàn)橡皮擦功能

標(biāo)簽:臺(tái)州 廣東 濟(jì)源 泰安 武威 安徽 濟(jì)寧 汕頭

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《基于canvas剪輯區(qū)域功能實(shí)現(xiàn)橡皮擦效果》,本文關(guān)鍵詞  基于,canvas,剪輯,區(qū)域,功能,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《基于canvas剪輯區(qū)域功能實(shí)現(xiàn)橡皮擦效果》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于基于canvas剪輯區(qū)域功能實(shí)現(xiàn)橡皮擦效果的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    鄱阳县| 和静县| 新邵县| 徐闻县| 治县。| 松溪县| 西城区| 玉树县| 勐海县| 阳朔县| 嘉善县| 沧州市| 宁都县| 兴仁县| 黎川县| 潞西市| 潼南县| 神农架林区| 民权县| 思茅市| 达孜县| 简阳市| 武川县| 额济纳旗| 金山区| 城口县| 佛山市| 贵溪市| 汽车| 红河县| 郑州市| 林西县| 盐边县| 新源县| 东乡县| 托里县| 安陆市| 海南省| 阳西县| 班戈县| 固阳县|