FULL STACK   ·   UI   ·   UX   ·   GRAPHICS   ·   DEVELOPER   ·   INSTRUCTOR

Adam Khoury

Donate funds to show love and support

Click the button below to donate funds securely online. You can use your PayPal account or a credit card.

Your donations help free up my time to produce more content and assist in covering server costs. It's also a great way to say thanks for the content!

Application Configuration

Adam will be adding options here soon.

Dynamic Centering and Alignment on the Canvas

Published :
Author :
Adam Khoury
In this JavaScript programming exercise you can learn the logic behind dynamic centering and alignment for dynamically positioning your canvas assets. <!DOCTYPE html> <html> <head> <style> body{ margin:10px; background:#666; } #my_canvas{ background:#FFF; border:#000 1px solid; } </style> <script> function draw(){ var ctx = document.getElementById('my_canvas').getContext('2d'); ctx.fillStyle = "magenta"; var rectW = 100; var rectH = 100; var rectX = (ctx.canvas.width * .5) - (rectW * .5); var rectY = (ctx.canvas.height * .5) - (rectH * .5); ctx.fillRect(rectX, rectY, rectW, rectH); } window.onload = draw; </script> </head> <body> <canvas id="my_canvas" width="500" height="350"></canvas> </body> </html>