gpt4 book ai didi

javascript - KineticJS:如何将 SVG Sprite 与 KineticJS Sprite 一起使用?

转载 作者:行者123 更新时间:2023-12-03 12:15:03 25 4
gpt4 key购买 nike

我有一个 SVG Sprite ,其架构如下:

Image Image Image  
Image Image Image
Image Image Image

其中 Image 是图像,所有九个图像都是 Sprite 。它们都在一层上。

有一个关于 PNG 文件 Sprite 的教程 here但我找不到如何使 SVG 文件工作。

使用 SVG Sprite 时如何设置框架的坐标?如何使用 SVG Sprite ?

编辑:这是本教程的代码。

<!DOCTYPE HTML>
<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
}
#container {
background-color: #222;
display: inline-block;
width: 580px;
height: 202px;
}
#buttons {
position: absolute;
top: 5px;
left: 10px;
}
#buttons > input {
padding: 10px;
display: block;
margin-top: 5px;
}
</style>
</head>
<body>
<div id="container"></div>
<div id="buttons">
<input type="button" id="punch" value="Punch">
</div>
<script src="http://d3lp1msu2r81bx.cloudfront.net/kjs/js/lib/kinetic-v5.0.2.min.js"></script>
<script defer="defer">
var stage = new Kinetic.Stage({
container: 'container',
width: 578,
height: 200
});
var layer = new Kinetic.Layer();

var imageObj = new Image();
imageObj.onload = function() {
var blob = new Kinetic.Sprite({
x: 250,
y: 40,
image: imageObj,
animation: 'idle',
animations: {
idle: [
// x, y, width, height (4 frames)
2,2,70,119,
71,2,74,119,
146,2,81,119,
226,2,76,119
],
punch: [
// x, y, width, height (3 frames)
2,138,74,122,
76,138,84,122,
346,138,120,122
]
},
frameRate: 7,
frameIndex: 0
});

// add the shape to the layer
layer.add(blob);

// add the layer to the stage
stage.add(layer);

// start sprite animation
blob.start();

var frameCount = 0;

blob.on('frameIndexChange', function(evt) {
if (blob.animation() === 'punch' && ++frameCount > 3) {
blob.animation('idle');
frameCount = 0;
}
});

document.getElementById('punch').addEventListener('click', function() {
blob.animation('punch');
}, false);
};
imageObj.src = 'http://www.html5canvastutorials.com/demos/assets/blob-sprite.png';
</script>
</body>
</html>

最佳答案

由于您没有提供代码,请单击 here了解这个想法并有一些关于 SVG 逐帧动画的不错的教程。但我永远不会推荐它,因为每个帧上都会有一个基于矢量的元素,其 anchor 、曲线等彼此独立运行。因此,对于要制作动画的一小部分,它必须一遍又一遍地加载所有点、颜色。您可以创建一个带有动画的 SVG。

关于javascript - KineticJS:如何将 SVG Sprite 与 KineticJS Sprite 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24758442/

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