--- title: Canvas API slug: Web/API/Canvas_API tags: - API - Canvas - NeedsTranslation - Overview - Reference - TopicStub translation_of: Web/API/Canvas_API ---
Доданий в HTML5, елемент HTML {{HTMLElement("canvas")}} призначений для створення графіки засобами JavaScript. Для прикладу, його використовують для малювання графіків, створення композиції фотографій, створення анімацій, і навіть для обробки та рендерингу відео в реальному часі.
Mozilla applications gained support for <canvas>
starting with Gecko 1.8 (i.e. Firefox 1.5). The element was originally introduced by Apple for the OS X Dashboard and Safari. Internet Explorer supports <canvas>
from version 9 onwards; for earlier versions of IE, a page can effectively add support for <canvas>
by including a script from Google's Explorer Canvas project. Google Chrome and Opera 9 also support <canvas>
.
The <canvas>
element is also used by WebGL to draw hardware-accelerated 3D graphics on web pages.
This is just a simple code snippet which uses the {{domxref("CanvasRenderingContext2D.fillRect()")}} method.
<canvas id="canvas"></canvas>
var canvas = document.getElementById('canvas'); var ctx = canvas.getContext('2d'); ctx.fillStyle = 'green'; ctx.fillRect(10, 10, 100, 100);
Edit the code below and see your changes update live in the canvas:
<canvas id="canvas" width="400" height="200" class="playable-canvas"></canvas> <div class="playable-buttons"> <input id="edit" type="button" value="Edit" /> <input id="reset" type="button" value="Reset" /> </div> <textarea id="code" class="playable-code"> ctx.fillStyle = 'green'; ctx.fillRect(10, 10, 100, 100);</textarea>
var canvas = document.getElementById('canvas'); var ctx = canvas.getContext("2d"); var textarea = document.getElementById('code'); var reset = document.getElementById('reset'); var edit = document.getElementById('edit'); var code = textarea.value; function drawCanvas() { ctx.clearRect(0, 0, canvas.width, canvas.height); eval(textarea.value); } reset.addEventListener('click', function() { textarea.value = code; drawCanvas(); }); edit.addEventListener('click', function() { textarea.focus(); }) textarea.addEventListener('input', drawCanvas); window.addEventListener('load', drawCanvas);
{{ EmbedLiveSample('Playable_code', 700, 360) }}
The interfaces related to the WebGLRenderingContext
are referenced under WebGL.
<canvas>
and its advanced features.<canvas>
.Specification | Status | Comment |
---|---|---|
{{SpecName('HTML WHATWG', 'scripting.html#the-canvas-element', '<canvas>')}} | {{Spec2('HTML WHATWG')}} |