HTML5 Google Webfonts

Monkey Forums/User Modules/HTML5 Google Webfonts

bruZard(Posted 2012) [#1]
just a small Lib to use Webfonts with Monkey.
var webFont			= '';
var webFontSize		= '12px';
var webFontColor	= 'rgba(2455, 255, 255, 1.0)';

function LoadWebFont(source, fontname, size){
	if(typeof fontname == 'string' && typeof size == 'number'){
		var style	= document.createElement('style');
		var font	= 'http://fonts.googleapis.com/css?family=' + source;
		var css		= document.createTextNode('@import url(' + font + ')');
		webFont		= fontname;
		webFontSize	= size + 'px';
		
		style.appendChild(css);
		document.getElementsByTagName('head')[0].appendChild(style);
	}
};

function Text(txt, x, y, style){
	game_canvas.getContext('2d').font = style + ' ' + webFontSize + ' ' + webFont;
	game_canvas.getContext('2d').fillText(txt, x, y);
};


Module:
Import "native/utils.js"

Extern
	Function LoadWebFont(source:String, fontname:String, size:Int) = "LoadWebFont"
	Function Text(txt:String, x:Int = 0, y:Int = 0, style:String = "normal") = "Text"
Public


use it:
Method OnCreate:Int()
	LoadWebFont("Inconsolata:400,700;", "Inconsolata", 18)
	SetUpdateRate(30)
	Return 0
End
	
Method OnUpdate:Int()
	Return 0
End
	
Method OnRender:Int()
	Cls()
	Text("Hello World", 10, 20, "700 italic")

	Return 0
End


have fun...


bruZard(Posted 2012) [#2]
Update:

Module: http://www.sedm.de/monkey/sedm.zip
Example:
Strict

Import mojo
Import sedm.utils

Class Test Extends App
	Field fnt:webFont
	Field mx:Int, my:Int
	
	Method OnCreate:Int()
		Self.fnt = LoadWebFont("Grand Hotel")
		FontSize(Self.fnt, 32)
		
		SetUpdateRate(30)
		
		Return 0
	End
	
	Method OnUpdate:Int()
		Self.mx = MouseX()
		Self.my = MouseY()
		
		Return 0
	End
	
	Method OnRender:Int()
		Cls(60, 90, 120)
		
		Text(Self.fnt, "Hallo Welt", 100, 150)
		
		Shadow(5, 5, 5, 0, 0, 0, 1.0)
		Text(Self.fnt, "Hallo Welt", Self.mx, Self.my)
		Shadow() ' reset shadow
		
		Return 0
	End
End

Function Main:Int()
	New Test()
	Return 0
End

Live: http://www.sedm.de/monkey/utils


Soap(Posted 2012) [#3]
This is excellent!


c.k.(Posted 2012) [#4]
If this could be used x-plat, it would be the bomb.


Shinkiro1(Posted 2012) [#5]
This helped me when making a small javascript game for school.
Thanks.