-
-
Save yushimatenjin/206ad29ad62fb18fb98cb658838357d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Replace2CanvasFont = pc.createScript('replace2canvasFont'); | |
// initialize code called once per entity | |
Replace2CanvasFont.prototype.initialize = function() { | |
this.app.root.findComponents('element').forEach(function(elm){ | |
if(elm.type === 'text') { | |
var font = new pc.CanvasFont(pc.app, { | |
fontName: 'UD Digi Kyokasho N-R, Helvetica, arial, sans-serif', | |
fontSize: 128, | |
}); | |
font.createTextures(elm.text); | |
elm.font = font; | |
} | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jshint esversion: 6, asi: true, laxbreak: true*/ | |
const Replace2CanvasFont = pc.createScript('replace2canvasFont'); | |
Replace2CanvasFont.attributes.add('fonts', { | |
type: 'string', | |
array: true, | |
default: ['https://fonts.googleapis.com/css2?family=Kosugi+Maru&display=swap'] | |
}); | |
// initialize code called once per entity | |
Replace2CanvasFont.prototype.initialize = function() { | |
if(this.fonts.length) { | |
const style = document.createElement('style'); | |
const fontnames = this.fonts.map(fonturl => new URL(fonturl).searchParams.get('family').split(':')[0]) | |
const fonturls = this.fonts.map(fonturl => '@import url(' + fonturl + ');') | |
style.innerHTML = fonturls.join('\n') | |
style.onload = () => document.fonts.load('12px ' + fontnames.join(', ')).then(fontFaces => { | |
const families = fontFaces.map(fontFace => fontFace.family) | |
this.replace(families) | |
}) | |
document.body.appendChild(style) | |
} else { | |
this.replace() | |
} | |
}; | |
Replace2CanvasFont.prototype.replace = function(families = []) { | |
this.app.root.findComponents('element').forEach(elm => { | |
if(elm.type === 'text') { | |
var font = new pc.CanvasFont(pc.app, { | |
fontName: [...families, 'arial', 'sans-serif'].join(', '), | |
fontSize: 128, | |
}); | |
font.createTextures(elm.text); | |
elm.font = font; | |
} | |
}) | |
} |
Author
yushimatenjin
commented
Dec 24, 2021
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment