繁体   English   中英

HTML5 Canvas Javascript库?

[英]Html5 Canvas Javascript libraries?

我想知道是否有任何JavaScript库可帮助渲染图形。 我搜索了google,但没有找到任何工具。 我想在画布上绘制高斯曲线。

我已经使用并推荐KineticJS库 它会定期进行积极维护和更新。

我花了15秒钟才找到。 试玩直到获得您喜欢的东西。

$(document).ready(drawGaussian);

var canvasContext;
var points;
var noise = 0;

function drawGaussian()
{   
canvasContext = document.getElementById("gaussian-canvas").getContext("2d");

document.getElementById("gaussian-canvas").onclick = cycleNoise;

cycleNoise();
}

function cycleNoise()
{   
canvasContext.clearRect(0, 0, 400, 400);

var m = Math.random() > .4
var amount = Math.round(Math.random() * 20000);
var size = Math.round(Math.random() * 3)+1;

document.getElementById("particles").innerHTML = amount;
document.getElementById("size").innerHTML = size;

switch(noise)
{
    case 0:
        drawGaussianField(amount, size, 200, 200, 400, 100, m);
        document.getElementById("type").innerHTML = (m ? "Monochromatic" : "Chromatic") + " Field";
        break;
    case 1:
        drawGaussianCurves(amount, size, 200, 200, 400, 150, m);
        document.getElementById("type").innerHTML = (m ? "Monochromatic" : "Chromatic") + " Curves";
        break;
    case 2:
        drawGaussianDiamond(amount, size, 200, 200, 400, 130, m);
        document.getElementById("type").innerHTML = (m ? "Monochromatic" : "Chromatic") + " Diamond";
        break;
    case 3:
        drawGaussianOval(amount, size, 200, 200, 300, 300, m);
        document.getElementById("type").innerHTML = (m ? "Monochromatic" : "Chromatic") + " Circle";
        break;
    case 4:
        drawGaussianBurst(amount, size, 200, 200, 120, 120, m);
        document.getElementById("type").innerHTML = (m ? "Monochromatic" : "Chromatic") + " Burst";
        break;
}

noise++;

if(noise > 4) noise = 0;
}


function drawGaussianField(amount, thickness, x, y, width, height, monochromatic)
{
for(i = 0; i < amount; i++)
{
    points = getGaussianPoints();

    setColor(monochromatic);
    canvasContext.fillRect(x + ((width*.5) * points[3]), y + ((height*.5) * points[2]), thickness, thickness);  
}
}

function drawGaussianCurves(amount, thickness, x, y, width, height, monochromatic){
for(i = 0; i < amount; i++)
{
    points = getGaussianPoints();

    setColor(monochromatic);
    canvasContext.fillRect(x + ((width*.5) * points[0]), y + ((height*.5) * points[2]), thickness, thickness);  
}
}

function drawGaussianDiamond(amount, thickness, x, y, width, height, monochromatic){
for(i = 0; i < amount; i++)
{
    points = getGaussianPoints();

    setColor(monochromatic);
    canvasContext.fillRect(x + ((width*.5) * points[0]), y + ((height*.5) * points[3]), thickness, thickness);  
}
}

function drawGaussianOval(amount, thickness, x, y, width, height, monochromatic){
for(i = 0; i < amount; i++)
{
    points = getGaussianPoints();

    setColor(monochromatic);
    canvasContext.fillRect(x + ((width*.5) * points[0]), y + ((height*.5) * points[1]), thickness, thickness);  
}
}

function drawGaussianBurst(amount, thickness, x, y, width, height, monochromatic){
for(i = 0; i < amount; i++)
{
    points = getGaussianPoints();

    setColor(monochromatic);
    canvasContext.fillRect(x + ((width*.5) * points[2]), y + ((height*.5) * points[3]), thickness, thickness);  
}
}

function setColor(val){
if(val)
{
    canvasContext.fillStyle = '#ffffff';
}
else
{
    canvasContext.fillStyle = "#"+Math.floor(Math.random()*16777215).toString(16);
}
}

function getGaussianPoints(){
var x1, x2, w, y1, y2;

do {
    x1 = 2.0 * Math.random() - 1.0;
    x2 = 2.0 * Math.random() - 1.0;
    w = x1 * x1 + x2 * x2;
} while ( w >= 1.0 );

w = Math.sqrt( (-2.0 * Math.log( w ) ) / w );
y1 = x1 * w;
y2 = x2 * w;

return [x1, x2, y1, y2];
}

有一些html5 canvas javascript库。 较完整的文件之一是paper.js

是在Smashing Magazine中将Raphael,Paper和Processing javascript库进行很好的比较

Paper.js非常适合HTML5画布。 如前所述,您应避免基于SVG或VML的库,因为大多数库在Android设备上均不起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM