2015-09-01 9 views
5

Ich habe dieses (run-Snippet)HTML Canvas, Unschärfen gezeichnet Polygon

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"\>

Ich muss nur eine Funktion hinzufügen können, um es auf der Website verwenden Ich baue es für. Einige der schwebenden Scherben müssen verschwommen sein, um ein Gefühl von Tiefe zu vermitteln.

Kann Canvas dies tun, und wenn ja, wie?

Danke!

+0

_Können Leinwand dies tun, und wenn ja, wie _ Versuchen [erkunden? it] (https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D) selbst – hindmost

+0

@markE Ich habe nicht gesagt, dass es ungültig ist, ich schlage nur das OP vor, zuerst die Lösung zu finden. – hindmost

+0

@markE Um das OP zu überzeugen, dass Canvas keine gebrauchsfertige Funktion/Methode hat – hindmost

Antwort

1

habe ich diese paar Monaten, vielleicht könnte es auch für Sie arbeiten:

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(); 
} 

Hier ist die Quelle: http://zurb.com/playground/image-blur-texture

+0

Ich habe das gesehen, aber ich glaube nicht, dass das mit meinen gezeichneten Polygonen funktioniert. Es verwischt auch alles ich glaube und ich möchte nur einige von ihnen verwischen – jansmolders86

+0

Ich bekomme es aber ich tat das gleiche wie Sie mit Punkten auf dem Bildschirm bewegen. Ich habe diese Funktion an bestimmten Punkten verwendet, damit Sie sie an bestimmte Polygone anpassen können. Fügen Sie eine Variable in der Funktion drawBlur hinzu, um bestimmte Werte des Bildes anstelle von "heroCanvas" zu übergeben. Ich fügte auch Variablen hinzu, um die Punkte progressiv zu verwischen. Ich habe diese Funktion geändert, um sie anzupassen. Anstelle von Bild können Sie die Funktion aufrufen, die Polygone erzeugt. – Majestic

+0

@Majestic, ich denke, Sie sind auf dem richtigen Weg, aber Ihre Demo zeigt einen leeren Bildschirm in Chrome, FF & IE auf Win10. – markE

Verwandte Themen