简体   繁体   English

如何在 Pixi.js 中制作手绘文本 animation?

[英]How can I make a handdrawn text animation in Pixi.js?

There's a handful of questions and answers for creating animations that mimic drawing or handwriting text .有一些关于创建模仿绘图或手写文本的动画的问题和答案。 Although I've found a canvas api example, and a svg-based solution, but I haven't seen anything using Pixi.js's API.虽然我找到了canvas api示例和基于svg 的解决方案,但我没有看到任何使用 Pixi.js 的 API 的解决方案。 I'm a rookie with canvas and Pixi.我是 canvas 和 Pixi 的菜鸟。

Here's the jsfiddle code:这是jsfiddle代码:

 var ctx = document.querySelector("canvas").getContext("2d"), dashLen = 220, dashOffset = dashLen, speed = 11, txt = "Wingardium Leviosa", x = 30, i = 0; ctx.font = "50px cursive, TSCu_Comic, sans-serif"; ctx.lineWidth = 5; ctx.lineJoin = "round"; ctx.globalAlpha = 2 / 3; ctx.strokeStyle = ctx.fillStyle = "black"; (function loop() { ctx.clearRect(x, 0, 60, 150); ctx.setLineDash([dashLen - dashOffset, dashOffset - speed]); // create a long dash mask ctx.setLineDash([dashLen - dashOffset, dashOffset]); // create a long dash mask dashOffset -= speed; // reduce dash length ctx.strokeText(txt[i], x, 90); // stroke letter if (dashOffset > 0) requestAnimationFrame(loop); // animate else { ctx.fillText(txt[i], x, 90); // fill final letter dashOffset = dashLen; // prep next char x += ctx.measureText(txt[i++]).width + ctx.lineWidth * Math.random(); ctx.setTransform(1, 0, 0, 1, 0, 3 * Math.random()); // random y-delta ctx.rotate(Math.random() * 0.005); // random rotation if (i < txt.length) { setTimeout(() => { requestAnimationFrame(loop); }, 300 * Math.random()) }; } })();
 <canvas width=600></canvas>

As I understand it, the canvas approach I liked to above is creating the draw effect largely from setLineDash which does not exist in Pixi.据我了解,我喜欢上面的 canvas 方法主要是从 Pixi 中不存在的setLineDash创建绘制效果。 I like this approach, I'm just not sure how to implement it in Pixi.我喜欢这种方法,我只是不确定如何在 Pixi 中实现它。

I don't know Pixi, but a viable solution could be to use the routine in your example against an hidden canvas, then using ctx.getImageData you could grab the result, finally to use the grabbed image as a Pixi texture.我不知道 Pixi,但一个可行的解决方案可能是使用您示例中的例程对隐藏的 canvas,然后使用ctx.getImageData您可以获取结果,最后将获取的图像用作 Pixi 纹理。

Another solution, maybe easier, but that can be only used when you don't need other Pixi effects (as an example, in a game, at game end or between two levels) you can stop the Pixi renderer and use the code you posted directly against Pixi canvas.另一种解决方案,可能更简单,但只能在不需要其他 Pixi 效果时使用(例如,在游戏中、游戏结束时或两个级别之间),您可以停止 Pixi 渲染器并使用您发布的代码直接针对 Pixi canvas。

Hope this helps.希望这可以帮助。

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

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