html文件:
<!DOCTYPE html> <html> <head> <title>A simple canvas Example</title> <style> body{ background: #dddddd; } #canvas { margin: 10px; padding: 10px; background: #ffffff; border: thin inset #aaaaaa; width:800px; height:300px; } </style> </head> <body> <canvas id = 'canvas' width = '600' height = '300'> Canvas not supported </canvas> <script src = 'example.js'></script> </body> </html>example.js
var canvas = document.getElementById('canvas'), context = canvas.getContext('2d'); context.font = '18px Arial'; context.fillStyle = 'cornflowerblue'; context.strokeStyle = 'green'; context.fillText('Hello Canvas1',canvas.width/2 - 111, canvas.height/2 + 15); context.font = '38px Arial'; context.strokeStyle = 'yellow'; context.strokeText('Hello Canvas22',canvas.width/2 - 150, canvas.height/2 + 150);