2015-09-01 122 views
5

我做了這個(運行下面摘錄)HTML畫布,畫的模糊多邊形

var Canvas = document.getElementById('c'); 
 
    var ctx = Canvas.getContext('2d'); 
 

 
    var resize = function() { 
 
     Canvas.width = Canvas.clientWidth; 
 
     Canvas.height = Canvas.clientHeight; 
 
    }; 
 
    window.addEventListener('resize', resize); 
 
    resize(); 
 

 
    var elements = []; 
 
    var presets = {}; 
 

 
    presets.shard = function (x, y, s, random, color) { 
 
     return { 
 
      x: x, 
 
      y: y, 
 
      draw: function(ctx, t) { 
 
       this.x += 0; 
 
       this.y += 0; 
 
       var posX = this.x + + Math.sin((50 + x + (t/10))/100) * 5; 
 
       var posy = this.y + + Math.sin((55 + x + (t/10))/100) * 7; 
 
       ctx.beginPath(); 
 
       ctx.fillStyle = color; 
 
       ctx.moveTo(posX, posy); 
 
       ctx.lineTo(posX+random,posy+random); 
 
       ctx.lineTo(posX+random,posy+random); 
 
       ctx.lineTo(posX+0,posy+50); 
 
       ctx.closePath(); 
 
       ctx.fill(); 
 
      } 
 
     } 
 
    }; 
 

 
    for(var x = 0; x < Canvas.width; x++) { 
 
     for(var y = 0; y < Canvas.height; y++) { 
 
      if(Math.round(Math.random() * 60000) == 1) { 
 
       var s = ((Math.random() * 5) + 1)/10; 
 
       if(Math.round(Math.random()) == 1){ 
 
        var random = Math.floor(Math.random() * 100) + 10; 
 
        var colorRanges = ['#8c8886', '#9c9995']; 
 
        var color = colorRanges[Math.floor(Math.random() * colorRanges.length)]; 
 
        elements.push(presets.shard(x, y, s, random, color)); 
 
       } 
 
      } 
 
     } 
 
    } 
 

 
    setInterval(function() { 
 
     ctx.clearRect(0, 0, Canvas.width, Canvas.height); 
 
     var time = new Date().getTime(); 
 
     for (var e in elements) 
 
      elements[e].draw(ctx, time); 
 
    }, 10);
<canvas id="c" width="1000" height="1000"\>

我只需要添加一個功能能夠使用它我建立的網站上爲了。一些浮動碎片需要模糊以給出深度感。

Can Canvas可以做到這一點,如果是這樣,如何?

謝謝!

+0

_Can帆布做到這一點,如果是這樣,如何_嘗試[探索?它](https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D)你自己 – hindmost

+0

@markE我沒有說它是無效的,我只是建議OP首先嚐試找到解決方案。 – hindmost

+0

@markE爲了說服OP,Canvas沒有現成的功能/方法 – hindmost

回答

1

我這個幾個月前使用,也許它會爲你工作,以及:

var canvas = document.getElementById("heroCanvas"); 
var canvasContext = canvas.getContext("2d"); 

var canvasBackground = new Image(); 
canvasBackground.src = "image.jpg"; 

var drawBlur = function() { 
    // Store the width and height of the canvas for below 
    var w = canvas.width; 
    var h = canvas.height; 
    // This draws the image we just loaded to our canvas 
    canvasContext.drawImage(canvasBackground, 0, 0, w, h); 
    // This blurs the contents of the entire canvas 
    stackBlurCanvasRGBA("heroCanvas", 0, 0, w, h, 100); 
} 

canvasBackground.onload = function() { 
    drawBlur(); 
} 

這裏來源:http://zurb.com/playground/image-blur-texture

+0

我看到了,但我不認爲這適用於我繪製的多邊形。 IT也模糊了我相信的東西,我想模糊其中的一些 – jansmolders86

+0

我明白了,但我做了與屏幕上移動的點相同的東西。 我在特定的點上使用了此功能,因此您可以將其調整爲特定的多邊形。在drawBlur函數中添加一個變量來傳遞圖像的特定值而不是「heroCanvas」。我還添加了變量以逐漸模糊點。 我已經修改了很多這個功能,使它適合。 而不是圖像,你可以稱之爲創建多邊形的函數。 – Majestic

+0

@Majestic,我認爲你在正確的軌道上,但是你的演示在Win10上的Chrome,FF和IE上顯示一個空白的屏幕。 – markE