Skip to content

Instantly share code, notes, and snippets.

@yushimatenjin
Last active February 8, 2021 10:08
Show Gist options
  • Save yushimatenjin/4252a43e216e025f00da28dc486399b7 to your computer and use it in GitHub Desktop.
Save yushimatenjin/4252a43e216e025f00da28dc486399b7 to your computer and use it in GitHub Desktop.
/*jshint esversion: 6, asi: true */
const EmojiRandom = pc.createScript('emojiRandom');
// initialize code called once per entity
EmojiRandom.prototype.initialize = function() {
setInterval(() => {
const emojiCode = Math.floor(Math.random() * (129685 - 129660 + 1) + 129660);
const emoji = String.fromCodePoint(emojiCode)
console.log(emoji)
this.app.fire("text:set", this.entity.element, emoji)
}, 200);
};
/*jshint esversion: 6, asi: true */
class FontManager extends pc.ScriptType {
get textElements() {
const elements = this.app.root.findComponents("element")
return elements || elements.map((element) => element.type === "text")
}
_canvasFontInit() {
const chars = []
for (let element of this.textElements) {
chars.push(element.text)
}
this._createTexture(chars.toString())
}
_createTexture(chars) {
if (this.app.canvasFont) {
this.app.canvasFont.destroy()
}
this.app.canvasFont = new pc.CanvasFont(pc.app, this.options)
this.app.canvasFont.createTextures(chars)
for (let element of this.textElements) {
element.font = this.app.canvasFont
}
}
_setText(element, char) {
if (this.app.canvasFont) {
this.app.canvasFont.updateTextures(char)
element.text = char
}
}
initialize() {
this.app.canvasFont = null
this.options = {
fontName: "Arial",
fontSize: 128,
}
this.app.on("text:set", this._setText, this)
this.on("attr", this._canvasFontInit, this)
this._canvasFontInit()
}
}
pc.registerScript(FontManager)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment