HTML5 3D circle text with shadows. Another one great HTML5 tutorial. It tells us how to draw 3D text and display it in a circle. Also it tells us about applying shadow and 3D effect to text. Plus I am going to rotate text in a circle.
Here are our demo and downloadable package:
[sociallocker]
[/sociallocker]
Ok, download the source files and lets start coding !
Step 1. HTML
As usual – very small HTML code with canvas element inside
index.html
04 | <meta charset="utf-8" /> |
05 | <title>HTML5 3D circle text with shadows | Script Tutorials</title> |
06 | <link href="css/main.css" rel="stylesheet" type="text/css" /> |
07 | <script type="text/javascript" src="js/script.js"></script> |
11 | <h2>HTML5 3D circle text with shadows</h2> |
14 | <div class="container"> |
16 | <input type="radio" name="mode" onchange="bPlay = false" checked /><label>Pause</label> |
17 | <input type="radio" name="mode" onchange="bPlay = true" /><label>Play</label> |
19 | <canvas id="panel" width="600" height="600"></canvas> |
Step 2. CSS
css/main.css
We are going to skip of publishing styles today again.
Step 3. JS
js/script.js
05 | var sText = 'Hello my 3D circle text '; |
08 | ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height); |
10 | function drawScene() { |
14 | ctx.fillStyle = '#d7e8f1'; |
15 | ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height); |
19 | draw3DTextCircle(sText, canvas.width / 2, canvas.height / 2, 200, Math.PI / 2 - iAngle); |
22 | function draw3DTextCircle(s, x, y, radius, iSAngle){ |
24 | var fRadPerLetter = 2*Math.PI / s.length; |
32 | ctx.fillStyle = '#168d1e'; |
34 | for (var i=0; i<s.length; i++) { |
36 | ctx.rotate(i*fRadPerLetter); |
38 | for (var n = 0; n < iDepth; n++) { |
39 | ctx.fillText(s[i], n, n - radius); |
42 | ctx.fillStyle = '#00d50f'; |
43 | ctx.shadowColor = 'black'; |
45 | ctx.shadowOffsetX = iDepth + 2; |
46 | ctx.shadowOffsetY = iDepth + 2; |
48 | ctx.fillText(s[i], 0, -radius); |
54 | if (window.attachEvent) { |
55 | window.attachEvent('onload', main_init); |
58 | var curronload = window.onload; |
59 | var newonload = function() { |
63 | window.onload = newonload; |
65 | window.onload = main_init; |
68 | function main_init() { |
70 | canvas = document.getElementById('panel'); |
71 | ctx = canvas.getContext('2d'); |
73 | ctx.font = '64px Verdana'; |
74 | ctx.textAlign = 'center'; |
75 | ctx.textBaseline = 'middle'; |
77 | ctx.fillStyle = '#d7e8f1'; |
78 | ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height); |
80 | draw3DTextCircle(sText, canvas.width / 2, canvas.height / 2, 200, Math.PI / 2 - iAngle); |
81 | setInterval(drawScene, 40); |
I have defined own function to draw 3d text circle – draw3DTextCircle. This function have next params – text itself, coordinates, radius and angle. This function will split all text by letters, then I will draw each letter separately with rotation (and 3d effect).
Conclusion
Welcome back to read something new and interesting about HTML5. Good luck in your projects.