gpt4 book ai didi

javascript - KineticJS 填充图案

转载 作者:行者123 更新时间:2023-12-02 18:17:25 24 4
gpt4 key购买 nike

我之前问过如何使用 html5 canvas 用棋盘格效果填充形状。 HTML5 Canvas Fill with two colours

我得到了一个 jsfiddle 来展示如何做到这一点。 http://jsfiddle.net/NdUcv/2/

var can = document.getElementById('canvas1');
var ctx = can.getContext('2d');

// set up a pattern, something really elaborate!
var pattern = document.createElement('canvas');
pattern.width = 40;
pattern.height = 40;
var pctx = pattern.getContext('2d');

pctx.fillStyle = "rgb(188, 222, 178)";
pctx.fillRect(0,0,20,20);
pctx.fillRect(20,20,20,20);

// Now we draw that pattern to a custom shape:

var pattern = ctx.createPattern(pattern, "repeat");
ctx.beginPath();
ctx.moveTo(30, 30);
ctx.lineTo(300, 30);
ctx.lineTo(400, 60);
ctx.lineTo(300, 150);
ctx.lineTo(200, 50);
ctx.lineTo(100, 450);
//ctx.closePath();
ctx.fillStyle = pattern;
ctx.fill();

我想知道如何将此语法转换为 KineticJS?我从kineticjs文档和示例中注意到,您可以填充图像或渐变,但没有提到填充图案。

如果可以用 html5 原生完成的话,这当然可以完成吗?

最佳答案

是的,您可以使用类似的代码来创建充满您的图案的动态多边形

enter image description here

用您的图案填充 Canvas ,就像在 native html Canvas 中一样:

    // use a temp canvas to create a pattern
var pattern = document.createElement('canvas');
pattern.width = 40;
pattern.height = 40;
var pctx = pattern.getContext('2d');
pctx.fillStyle = "rgb(188, 222, 178)";
pctx.fillRect(0,0,20,20);
pctx.fillRect(20,20,20,20);

现在使用临时 Canvas 创建图像对象

    var img=new Image();
img.onload=function(){
// img now contains your pattern
}
img.src=pattern.toDataURL();

最后,使用 fillPatternImage 用您的图案填充动态多边形:

        // make a kinetic polygon filled with the pattern
var polyPattern = new Kinetic.Polygon({
points: [30,30, 300,30, 400,60, 300,150, 200,50, 100,450],
fillPatternImage: img,
stroke: 'black',
strokeWidth: 3
});

这是代码和 fiddle :http://jsfiddle.net/m1erickson/uW8xz/

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Prototype</title>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v4.7.0.min.js"></script>

<style>
#container{
border:solid 1px #ccc;
margin-top: 10px;
width:400px;
height:400px;
}
</style>
<script>
$(function(){

var stage = new Kinetic.Stage({
container: 'container',
width: 450,
height: 450
});
var layer = new Kinetic.Layer();
stage.add(layer);

// use a temp canvas to create a pattern
var pattern = document.createElement('canvas');
pattern.width = 40;
pattern.height = 40;
var pctx = pattern.getContext('2d');
pctx.fillStyle = "rgb(188, 222, 178)";
pctx.fillRect(0,0,20,20);
pctx.fillRect(20,20,20,20);

// make an image from the temp canvas pattern
var img=new Image();
img.onload=function(){

// make a kinetic polygon filled with the pattern
var polyPattern = new Kinetic.Polygon({
points: [30,30, 300,30, 400,60, 300,150, 200,50, 100,450],
fillPatternImage: img,
stroke: 'black',
strokeWidth: 3
});
// add the shape to the layer and layer.draw()
layer.add(polyPattern);
layer.draw();

}
img.src=pattern.toDataURL();


}); // end $(function(){});

</script>
</head>

<body>
<div id="container"></div>
</body>
</html>

关于javascript - KineticJS 填充图案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19163143/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com