2016-08-12 1 views
2

내가 만들고 싶어 지우기흐림 효과 효과는 JQuery 또는 Javascript에서 지우기. 마우스를 가져 가면 흐려진 이미지를 지우고 흐려지지 않은 이미지를 표시하는 코드를 작성했습니다. 여기에 스크린 샷이 있습니다. enter image description hereJQuery 또는 Javascript에서 흐리게 지우개 및 흐리게 그리기 효과를 만드는 방법은 무엇입니까?

이미지 위로 마우스를 가져 가면 흐림 이미지를 지울 수 있지만 같은 위치에서 흐린 이미지를 다시 그리지 못했습니다. 500 밀리 초 후에 나타날 수 있습니다. 어떻게하면 마우스로 맴돌았을 때 다시 블러를 다시 그릴 수 있습니까?

여기 참조 용 코드입니다 :

<!DOCTYPE html> 
<html> 
<head> 
    <title>Blur Testing</title> 
    <meta name="content-type" content="text/html; charset=UTF-8"> 
    <style> 
     body { 
      margin: 0; 
     } 
     #item { 
      background: url("http://i66.tinypic.com/2z6uq9f.jpg"); 
      background-size: cover; 
      background-position: center; 
     } 
    </style> 
    <script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script> 
</head> 
<body> 
<canvas id="item"></canvas> 
<script> 
    var canvas = document.getElementById('item'); 
    var ctx = canvas.getContext('2d'), 
      img = new Image, 
      radius = 30; 
    canvas.width = window.innerWidth; 
    canvas.height = window.innerHeight; 

    $('#item').css({ 
     "-webkit-filter": "blur(0px)", 
     "filter": "blur(0px)" 
    }); 
    $(img).on('load', function() { 
     $('#item').mouseover(function (e) { 
      erase(getXY(e)); 
     }).mousemove(function (e) { 
      erase(getXY(e)); 
      //setTimeout(redraw(getXY(e)), 400); 
     }); 

     ctx.drawImage(img, 0, 0); 
     ctx.globalCompositeOperation = 'destination-out'; 
    }); 
    img.src = 'http://i64.tinypic.com/14mt7yx.jpg'; 
    img.width = window.width; 
    img.height = window.height; 

    function getXY(e) { 
     var r = $('#item')[0].getBoundingClientRect(); 
     return {x: e.clientX - r.left, y: e.clientY - r.top}; 
    } 
// function redraw(pos) { 
//  ctx.globalCompositeOperation = 'source-in'; 
//  ctx.beginPath(); 
//  ctx.arc(pos.x, pos.y, radius, 0, 2 * Math.PI); 
//  ctx.closePath(); 
//  ctx.fill(); 
// }; 
    function erase(pos) { 
     ctx.globalCompositeOperation = 'destination-out'; 
     ctx.beginPath(); 
     ctx.arc(pos.x, pos.y, radius, 0, 2 * Math.PI); 
     ctx.closePath(); 
     ctx.fill(); 
    } 
</script> 
</body> 
</html> 

답변

2

다음은 Alpha Mask Filter 질문에 대한 빠른 수정입니다. 나는 단순히 흐림과 선명한 이미지를 뒤집었다. 자세한 내용은 링크 된 질문을 참조하십시오.

여분 비트

는 흐림

blurMaskFadeCounter += 1; 
    if((blurMaskFadeCounter % blurMaskFadeRate) === 0){ 
     maskImage.ctx.globalCompositeOperation = "destination-out"; 
     maskImage.ctx.fillStyle = "#000"; 
     maskImage.ctx.globalAlpha = 0.1; 
     maskImage.ctx.fillRect(0,0,maskImage.width,maskImage.height); 
     maskImage.ctx.globalAlpha = 1; 
     maskImage.ctx.globalCompositeOperation = "source-over"; 
    } 

간단히 낮게 설정 대상 아웃 조성물 알파 마스크 위에 무 마스크 페이드 아웃한다. 속도를 늦추려면 너무 많은 프레임마다 타이밍이 맞아야합니다. 당신은 0.1 아래 알파를 설정하면 프레임 스킵 내가 마우스를 가져가 클릭 대신으로 변경하고 드래그 어떻게 느린 응답

var imageLoadedCount = 0; 
 
var error = false; 
 
var maskImage; 
 
var flowerImage; 
 
var flowerImageBlur; 
 
/** ImageTools.js begin **/ 
 
var imageTools = (function() { 
 
    var tools = { 
 
     canvas : function (width, height) { // create a blank image (canvas) 
 
      var c = document.createElement("canvas"); 
 
      c.width = width; 
 
      c.height = height; 
 
      return c; 
 
     }, 
 
     createImage : function (width, height) { 
 
      var image = this.canvas(width, height); 
 
      image.ctx = image.getContext("2d"); 
 
      return image; 
 
     }, 
 
     loadImage : function (url, callback) { 
 
      var image = new Image(); 
 
      image.src = url; 
 
      image.addEventListener('load', callback); 
 
      image.addEventListener('error', callback); 
 
      return image; 
 
     } 
 
    }; 
 
    return tools; 
 
})(); 
 

 

 

 

 
var mouse; 
 
var demo = function(){ 
 
    /** fullScreenCanvas.js begin **/ 
 
    var canvas = (function(){ 
 
     var canvas = document.getElementById("canv"); 
 
     if(canvas !== null){ 
 
      document.body.removeChild(canvas); 
 
     } 
 
     // creates a blank image with 2d context 
 
     canvas = document.createElement("canvas"); 
 
     canvas.id = "canv";  
 
     canvas.width = window.innerWidth; 
 
     canvas.height = window.innerHeight; 
 
     canvas.style.position = "absolute"; 
 
     canvas.style.top = "0px"; 
 
     canvas.style.left = "0px"; 
 
     canvas.style.zIndex = 1000; 
 
     canvas.ctx = canvas.getContext("2d"); 
 
     document.body.appendChild(canvas); 
 
     return canvas; 
 
    })(); 
 
    var ctx = canvas.ctx; 
 
    
 
    /** fullScreenCanvas.js end **/ 
 
    /** MouseFull.js begin **/ 
 
    if(typeof mouse !== "undefined"){ // if the mouse exists 
 
     if(mouse.removeMouse !== undefined){ 
 
      mouse.removeMouse(); // remove previouse events 
 
     } 
 
    }else{ 
 
     var mouse; 
 
    } 
 
    var canvasMouseCallBack = undefined; // if needed 
 
    mouse = (function(){ 
 
     var mouse = { 
 
      x : 0, y : 0, w : 0, alt : false, shift : false, ctrl : false, 
 
      interfaceId : 0, buttonLastRaw : 0, buttonRaw : 0, 
 
      over : false, // mouse is over the element 
 
      bm : [1, 2, 4, 6, 5, 3], // masks for setting and clearing button raw bits; 
 
      getInterfaceId : function() { return this.interfaceId++; }, // For UI functions 
 
      startMouse:undefined, 
 
      mouseEvents : "mousemove,mousedown,mouseup,mouseout,mouseover,mousewheel,DOMMouseScroll".split(",") 
 
     }; 
 
     function mouseMove(e) { 
 
      var t = e.type, m = mouse; 
 
      m.x = e.offsetX; m.y = e.offsetY; 
 
      if (m.x === undefined) { m.x = e.clientX; m.y = e.clientY; } 
 
      m.alt = e.altKey;m.shift = e.shiftKey;m.ctrl = e.ctrlKey; 
 
      if (t === "mousedown") { m.buttonRaw |= m.bm[e.which-1]; 
 
      } else if (t === "mouseup") { m.buttonRaw &= m.bm[e.which + 2]; 
 
      } else if (t === "mouseout") { m.buttonRaw = 0; m.over = false; 
 
      } else if (t === "mouseover") { m.over = true; 
 
      } else if (t === "mousewheel") { m.w = e.wheelDelta; 
 
      } else if (t === "DOMMouseScroll") { m.w = -e.detail;} 
 
      if (canvasMouseCallBack) { canvasMouseCallBack(mouse); } 
 
      e.preventDefault(); 
 
     } 
 
     function startMouse(element){ 
 
      if(element === undefined){ 
 
       element = document; 
 
      } 
 
      mouse.element = element; 
 
      mouse.mouseEvents.forEach(
 
       function(n){ 
 
        element.addEventListener(n, mouseMove); 
 
       } 
 
      ); 
 
      element.addEventListener("contextmenu", function (e) {e.preventDefault();}, false); 
 
     } 
 
     mouse.removeMouse = function(){ 
 
      if(mouse.element !== undefined){ 
 
       mouse.mouseEvents.forEach(
 
        function(n){ 
 
         mouse.element.removeEventListener(n, mouseMove); 
 
        } 
 
       ); 
 
       canvasMouseCallBack = undefined; 
 
      } 
 
     } 
 
     mouse.mouseStart = startMouse; 
 
     return mouse; 
 
    })(); 
 
    if(typeof canvas !== "undefined"){ 
 
     mouse.mouseStart(canvas); 
 
    }else{ 
 
     mouse.mouseStart(); 
 
    } 
 
    /** MouseFull.js end **/ 
 
    
 
    // load the images and create the mask 
 
    if(imageLoadedCount === 0){ 
 
     imageLoadedCount = 0; 
 
     error = false; 
 
     maskImage; 
 
     flowerImage = imageTools.loadImage("http://www.createjs.com/demos/_assets/art/flowers.jpg", function (event) { 
 
      if (event.type === "load") { 
 
       imageLoadedCount += 1; 
 
      } else { 
 
       error = true; 
 
      } 
 
     }) 
 
     flowerImageBlur = imageTools.loadImage("http://i.stack.imgur.com/3S5m8.jpg", function() { 
 
      if (event.type === "load") { 
 
       maskImage = imageTools.createImage(this.width, this.height); 
 
       imageLoadedCount += 1; 
 
      } else { 
 
       error = true; 
 
      } 
 
     }) 
 
    } 
 
    // set up the canvas 
 
    var w = canvas.width; 
 
    var h = canvas.height; 
 
    var cw = w/2; 
 
    var ch = h/2; 
 

 

 
    // calculate time to download image using the MS algorithum. As this code is a highly gaurded secret I have obsficated it for your personal safty. 
 
    var calculateTimeToGo= (function(){var b="# [email protected],Some time soon,Maybe Tomorrow.".replace(/Q/g,"@.,# ").split(","),r=Math.random,f=Math.floor,lc=0,pc=0,lt=0,lp=0;var cttg=function(a){if(lc===0){lc=100+r(r()*60);lt=f(r()*40);if(pc===0||r()<(lp/b.length)-0.2){lp=f(r()*b.length);pc=1+f(r()*10)}else{pc-=1}}else{lc-=1}a=lt;if(lp===0){a=lt;if(r()<0.01){lt-=1}}var s=b[lp].replace("#",a);if(a===1){s=s.replace("@","")}else{s=s.replace("@","s")}return s};return cttg})();  
 

 
    // draws circle with gradient 
 
    function drawCircle(ctx, x, y, r) { 
 
     var gr = ctx.createRadialGradient(x, y, 0, x, y, r) 
 
      gr.addColorStop(1, "rgba(0,0,0,0)") 
 
      gr.addColorStop(0.5, "rgba(0,0,0,0.08)") 
 
      gr.addColorStop(0, "rgba(0,0,0,0.1)") 
 
      ctx.fillStyle = gr; 
 
     ctx.beginPath(); 
 
     ctx.arc(x, y, r, 0, Math.PI * 2); 
 
     ctx.fill(); 
 
    } 
 
    // draw text 
 
    function drawText(ctx, text, size, x, y, c) { 
 
     ctx.fillStyle = c; 
 
     ctx.strokeStyle = "black"; 
 
     ctx.lineWidth = 5; 
 
     ctx.lineJoin = "round"; 
 
     ctx.font = size + "px Arial Black"; 
 
     ctx.textAlign = "center"; 
 
     ctx.textBaseline = "middle"; 
 
     if (c !== "black") { 
 
      ctx.strokeText(text, x, y + 1); 
 
     } 
 
     ctx.fillText(text, x, y); 
 
    } 
 
    // draw the image to fit the current canvas size 
 
    function drawImageCentered(ctx, image, x, y) { 
 
     var scale = Math.min(w/image.width, h/image.height); 
 
     ctx.setTransform(scale, 0, 0, scale, cw, ch); 
 
     ctx.drawImage(image, -image.width/2, -image.height/2); 
 
     ctx.setTransform(1, 0, 0, 1, 0, 0); 
 
    } 
 
    // how often to fade blur mask 
 
    var blurMaskFadeRate =8; // number of frames between fading mask out 
 
    var blurMaskFadeCounter = 0; 
 
    // points for filling gaps between mouse moves. 
 
    var lastMX,lastMY; 
 
    // update function will try 60fps but setting will slow this down.  
 
    function update(time){ 
 
     ctx.setTransform(1, 0, 0, 1, 0, 0); // restore transform 
 
     ctx.clearRect(0, 0, w, h); // clear rhe canvas 
 
     // have the images loaded??? 
 
     if (imageLoadedCount === 2) { 
 
      // draw the unblured image that will appear at the top 
 
      ctx.globalCompositeOperation = "source-over"; 
 
      drawImageCentered(ctx, flowerImageBlur, cw, ch); 
 
      drawText(ctx, "Move mouse over image to unblur.", 20 + Math.sin(time/100), cw, ch - 30, "White"); 
 
      // Mask out the parts when the mask image has pixels 
 
      ctx.globalCompositeOperation = "destination-out"; 
 
      drawImageCentered(ctx, maskImage, cw, ch); 
 
      // draw the blured image only where the destination has been masked 
 
      ctx.globalCompositeOperation = "destination-atop"; 
 

 
      drawImageCentered(ctx, flowerImage, cw, ch); 
 
      
 
      blurMaskFadeCounter += 1; 
 
      if((blurMaskFadeCounter % blurMaskFadeRate) === 0){ 
 
       maskImage.ctx.globalCompositeOperation = "destination-out"; 
 
       maskImage.ctx.fillStyle = "#000"; 
 
       maskImage.ctx.globalAlpha = 0.1; 
 
       maskImage.ctx.fillRect(0,0,maskImage.width,maskImage.height); 
 
       maskImage.ctx.globalAlpha = 1; 
 
       maskImage.ctx.globalCompositeOperation = "source-over"; 
 
      } 
 

 
      // because image has been scaled need to get mouse coords on image 
 
      var scale = Math.min(w/flowerImage.width, h/flowerImage.height); 
 
      var x = (mouse.x - (cw - (maskImage.width/2) * scale))/scale; 
 
      var y = (mouse.y - (ch - (maskImage.height/2) * scale))/scale; 
 
      // draw circle on mask 
 
      drawCircle(maskImage.ctx, x, y, 60); 
 
      // if mouse is draging then draw some points between to fill the gaps 
 
      if (lastMX !== undefined) { 
 
       drawCircle(maskImage.ctx, ((x + lastMX)/2 + x)/2, ((y + lastMY)/2 + y)/2, 60); 
 
       drawCircle(maskImage.ctx, (x + lastMX)/2, (y + lastMY)/2, 60); 
 
       drawCircle(maskImage.ctx, ((x + lastMX)/2 + lastMX)/2, ((y + lastMY)/2 + lastMY)/2, 60); 
 
      } 
 
      // save las mouse pos on image 
 
      lastMX = x; 
 
      lastMY = y; 
 
     } else { 
 
      // Laoding images so please wait. 
 
      drawText(ctx, "Please wait.", 40 + Math.sin(time/100), cw, ch - 30, "White"); 
 
      drawText(ctx, "loading images... ", 12, cw, ch, "black") 
 
      drawText(ctx, "ETA " + calculateTimeToGo(time), 14, cw, ch + 20, "black") 
 
     } 
 
     
 
     // if not restart the request animation frame 
 
     if(!STOP){ 
 
      requestAnimationFrame(update); 
 
     }else{ 
 
      var can = document.getElementById("canv"); 
 
      if(can !== null){ 
 
       document.body.removeChild(can); 
 
      }   
 
      STOP = false; 
 
      
 
     } 
 
    } 
 

 
    update(); 
 
    
 
} 
 
var STOP = false; // flag to tell demo app to stop 
 
function resizeEvent() { 
 
    var waitForStopped = function() { 
 
     if (!STOP) { // wait for stop to return to false 
 
      demo(); 
 
      return; 
 
     } 
 
     setTimeout(waitForStopped, 200); 
 
    } 
 
    STOP = true; 
 
    setTimeout(waitForStopped, 100); 
 
} 
 
window.addEventListener("resize", resizeEvent); 
 
demo();

+0

을 줄 수 있도록 완전히 명확 못해 일부 픽셀을 얻을? –

+0

@MallikCheripally 업데이트 함수 – Blindman67

+0

에서'if (mouse.buttonRaw === 1) {'와'} else {lastMX = undefined;}'를 제거하십시오. 만약'if (mouse.buttonRaw === 1) {. ..} else {}'그 다음에 오류가 발생 함 'Uncaught SyntaxError : 예기치 않은 입력의 끝' –

관련 문제