-
-
Save niceaji/aa178ecde02d5db1db63 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
/* | |
* draw a multiline string rotated in a canvas | |
* | |
* @param ctx (M) context of the canvas | |
* @param text (M) string may contain \n | |
* @param posX (M) horizontal start position | |
* @param posY (M) vertical start position | |
* @param textColor color | |
* @param rotation in degrees (by 360) | |
* @param font must be installed on client use websafe | |
* @param fonSize in Pixels | |
* | |
* all (M) params are mandatory - rest is optional | |
*/ | |
function drawString(ctx, text, posX, posY, textColor, rotation, font, fontSize) { | |
var lines = text.split("\n"); | |
if (!rotation) rotation = 0; | |
if (!font) font = "'serif'"; | |
if (!fontSize) fontSize = 16; | |
if (!textColor) textColor = '#000000'; | |
ctx.save(); | |
ctx.font = fontSize + "px " + font; | |
ctx.fillStyle = textColor; | |
ctx.translate(posX, posY); | |
ctx.rotate(rotation * Math.PI / 180); | |
for (i = 0; i < lines.length; i++) { | |
ctx.fillText(lines[i],0, i*fontSize); | |
} | |
ctx.restore(); | |
} | |
/* end */ |
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
<html> | |
<head> | |
<title>test</title> | |
<script type="text/javascript"> | |
/* | |
* draw a multiline string rotated in a canvas | |
* | |
* @param ctx (M) context of the canvas | |
* @param text (M) string may contain \n | |
* @param posX (M) horizontal start position | |
* @param posY (M) vertical start position | |
* @param textColor color | |
* @param rotation in degrees (by 360) | |
* @param font must be installed on client use websafe | |
* @param fonSize in Pixels | |
* | |
* all (M) params are mandatory - rest is optional | |
*/ | |
function drawString(ctx, text, posX, posY, textColor, rotation, font, fontSize) { | |
var lines = text.split("\n"); | |
if (!rotation) rotation = 0; | |
if (!font) font = "'serif'"; | |
if (!fontSize) fontSize = 16; | |
if (!textColor) textColor = '#000000'; | |
ctx.save(); | |
ctx.font = fontSize + "px " + font; | |
ctx.fillStyle = textColor; | |
ctx.translate(posX, posY); | |
ctx.rotate(rotation * Math.PI / 180); | |
for (i = 0; i < lines.length; i++) { | |
ctx.fillText(lines[i],0, i*fontSize); | |
} | |
ctx.restore(); | |
} | |
function run() { | |
var nbc = document.getElementById("nb").getContext('2d'); | |
drawString(nbc, 'hallo wie gehts?\ndas ist ein Winkel von 30 Grad!', 200, 200, '#444',30,"Chalkduster",36); | |
drawString(nbc, 'auf\'m Kopf', 500, 100, '#363',180,"Chalkduster",24); | |
drawString(nbc, 'und alles mit HTML5 JS coool',500,300,'#a66',-30,"Trebuchet MS",24); | |
drawString(nbc, 'nach unten',10,10,'#66a',90,"Trebuchet MS",24); | |
drawString(nbc, 'nach oben',27,590,'#66a',-90,"sans-serif",24); | |
} | |
</script> | |
</head> | |
<body onload="run();"> | |
<canvas id="nb" width="800" height="600"></canvas> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment