<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>phaserjs &#8211; Filip Raiper34 Gulan</title>
	<atom:link href="https://gulan.eu/tag/phaserjs/feed/" rel="self" type="application/rss+xml" />
	<link>https://gulan.eu</link>
	<description>Senior frontend developer</description>
	<lastBuildDate>Fri, 07 Jul 2023 21:06:59 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Material Design Icons in the Phaser game</title>
		<link>https://gulan.eu/material-design-icons-in-the-phaser-game/</link>
					<comments>https://gulan.eu/material-design-icons-in-the-phaser-game/#respond</comments>
		
		<dc:creator><![CDATA[Raiper34]]></dc:creator>
		<pubDate>Fri, 07 Jul 2023 14:24:33 +0000</pubDate>
				<category><![CDATA[Frameworks]]></category>
		<category><![CDATA[Game development]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Phaserjs]]></category>
		<category><![CDATA[Typescript]]></category>
		<category><![CDATA[game development]]></category>
		<category><![CDATA[google font icons]]></category>
		<category><![CDATA[google font icons in Phaser]]></category>
		<category><![CDATA[google font icons in Phaser3]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[phaser]]></category>
		<category><![CDATA[phaser3]]></category>
		<category><![CDATA[phaserjs]]></category>
		<category><![CDATA[typescript]]></category>
		<guid isPermaLink="false">https://gulan.eu/?p=311</guid>

					<description><![CDATA[<span class="span-reading-time rt-reading-time" style="display: block;"><span class="rt-label rt-prefix">Reading Time: </span> <span class="rt-time"> 3</span> <span class="rt-label rt-postfix">minutes</span></span>During my upcoming game development, I have realized that paying a graphic designer for every single thing is quite time-consuming and also a bit expensive. I needed a simple button for a fullscreen toggle. Some time ago, I needed to rework all buttons in my game. All buttons have got text inside the image, but [&#8230;]]]></description>
										<content:encoded><![CDATA[<span class="span-reading-time rt-reading-time" style="display: block;"><span class="rt-label rt-prefix">Reading Time: </span> <span class="rt-time"> 3</span> <span class="rt-label rt-postfix">minutes</span></span>
<p>During my upcoming game development, I have realized that paying a graphic designer for every single thing is quite time-consuming and also a bit expensive. I needed a simple button for a fullscreen toggle. Some time ago, I needed to rework all buttons in my game. All buttons have got text inside the image, but I needed to separate it, because of localization. So I separated all text from image graphics and create <strong>JSON i18n</strong> files for every language that I wanted to include in the game. Ok well, but what about icons? Do I need to pay every time to the graphic designer when I need a new button? I have a button image (background), I need only an icon. Yes, I can create icons on my own. Or? Hmmm, There is another approach. What about using some web icon fonts like FontAwesome, Google Material Design Icons, or similar? Yes, that way exists!</p>



<p>Let&#8217;s say, we have a working Phaser 3 project. First download <strong>.tff</strong> font from the original Material Design Icons repository <a rel="noreferrer noopener" href="https://github.com/google/material-design-icons/blob/master/font/MaterialIcons-Regular.ttf" target="_blank">https://github.com/google/material-design-icons/blob/master/font/MaterialIcons-Regular.ttf</a>. Add font inside your project, I put it into the <strong>assets </strong>folder. Now we need to load font to be able to use it inside the Phaser project. We can load it in a CSS file and then create a fake HTML element, but there is a better newer approach called <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/API/CSS_Font_Loading_API" data-type="URL" data-id="https://developer.mozilla.org/en-US/docs/Web/API/CSS_Font_Loading_API" target="_blank">CSS Font loading API</a>. Thanks to this API, we can dynamically load fonts in Javascript. So we load font. </p>



<pre class="wp-block-code"><code lang="typescript" class="language-typescript line-numbers">const font = new FontFace("Icons", "url('./assets//MaterialIcons-Regular.ttf')");
(document.fonts as any).add(font);
font.load();

document.fonts.ready.then(() =&gt; {
      // Use your font here
});</code></pre>



<p></p>



<p>The first argument of <strong>FontFace</strong> is font name, it is user-defined, and you can use your own idea. The second argument is the path to your downloaded font, as I said, I put it into the assets folder. Then you need to add it into document fonts and load it. Font loading is an asynchronous operation, so we can not use it immediately, but need to wait for the font to be loaded. We can use our font in <strong>ready</strong> promise, or we can load it in some preload state during assets loading. </p>



<p>Now we can pick our desired icon on <a rel="noreferrer noopener" href="https://fonts.google.com/icons?selected=Material+Icons+Outlined:home:&amp;icon.style=Outlined" target="_blank">https://fonts.google.com/icons?selected=Material+Icons+Outlined:home:&amp;icon.style=Outlined</a> and copy th<strong>e Code point</strong>. </p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="1025" height="685" src="https://gulan.eu/wp-content/uploads/2023/07/google-font-icons.png" alt="" class="wp-image-316" srcset="https://gulan.eu/wp-content/uploads/2023/07/google-font-icons.png 1025w, https://gulan.eu/wp-content/uploads/2023/07/google-font-icons-300x200.png 300w, https://gulan.eu/wp-content/uploads/2023/07/google-font-icons-768x513.png 768w" sizes="(max-width: 1025px) 100vw, 1025px" /></figure>



<p>Finally, we can create our material icon in Phaser.</p>



<pre class="wp-block-code"><code lang="typescript" class="language-typescript line-numbers">const text = this.add.text(0, 0, '\ue8b8', {color: '#6002EE', fontSize: '50px', fontFamily: 'Icons', padding: {left: 5, right: 5, top: 5, bottom: 5}});</code></pre>



<p>Important here is the content of the text (3rd argument), which is copied <strong>code point</strong> with <strong>\u</strong> prefix, which means Unicode. Also, we need to specify, that text uses our font, see font family, with our defined font name. Almost everything is good, but sometimes some icons got their top cut off. I do not understand why, but we can fix it using padding, and try to experiment with this property. I used 5px. </p>



<p>That was not so hard, and I think, I will use this approach more often. The good part is, I can combine it with the button background image and style icon in Phaser and avoid the need for a graphic designer. Pretty cool, isn&#8217;t it? You do not need to use only Material design icons, but any font icons on the web,  like Font Awesome&#8230; Just care font license!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://gulan.eu/material-design-icons-in-the-phaser-game/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
