Элемент {{HTMLElement("canvas")}} – это HTML-элемент, который может использоваться для рисования графики с помощью скриптов (обычно это JavaScript). Например, он может использоваться для рисования графиков, создания фотокомпозиций и анимаций или даже обработки видео в реальном времени.
Приложения от Mozilla поддерживают <canvas>
начиная с Gecko 1.8 (т.е. с Firefox 1.5). Этот элемент первоначально был внедрен Apple для OS X Dashboard и Safari. Internet Explorer поддерживает <canvas>
начиная с 9 версии и новее; для более ранних версий IE поддержку для <canvas> можно добавить с помощью скрипта из проекта Google's Explorer Canvas. Google Chrome и Opera 9 также поддерживают <canvas>
.
Элемент <canvas>
также используется технологией WebGL чтобы аппаратно ускорять 3D графику на вебстраницах.
Example
This is just a simple code snippet which uses the {{domxref("CanvasRenderingContext2D.fillRect()")}} method.
HTML
<canvas id="canvas"></canvas>
JavaScript
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:
{{ EmbedLiveSample('Playable_code', 700, 360) }}
Reference
- {{domxref("HTMLCanvasElement")}}
- {{domxref("CanvasRenderingContext2D")}}
- {{domxref("CanvasGradient")}}
- {{domxref("CanvasPattern")}}
- {{domxref("ImageBitmap")}}
- {{domxref("ImageData")}}
- {{domxref("TextMetrics")}}
- {{domxref("Path2D")}} {{experimental_inline}}
The interfaces related to the WebGLRenderingContext
are referenced under WebGL.
Guides and tutorials
- Canvas tutorial
- A comprehensive tutorial covering both the basic usage of and its advanced features.
- Code snippets: Canvas
- Some extension developer-oriented code snippets involving .
- Demo: A basic ray-caster
- A demo of ray-tracing animation using canvas.
- Drawing DOM objects into a canvas
- How to draw DOM content, such as HTML elements, into a canvas.
- Manipulating video using canvas
- Combining {{HTMLElement("video")}} and {{HTMLElement("canvas")}} to manipulate video data in real time.
Resources
Generic
Libraries
- Fabric.js is an open-source canvas library with SVG parsing capabilities.
- Kinetic.js is an open-source canvas library focused on interactivity for desktop and mobile applications.
- Paper.js is an open source vector graphics scripting framework that runs on top of the HTML5 Canvas.
- libCanvas is powerful and lightweight canvas framework.
- Processing.js is a port of the Processing visualization language.
- PlayCanvas is an open source game engine.
- Pixi.js is an open source game engine.
- PlotKit is a charting and graphing library.
- Rekapi is an animation key-framing API for Canvas.
- PhiloGL is a WebGL framework for data visualization, creative coding and game development.
- JavaScript InfoVis Toolkit creates interactive 2D Canvas data visualizations for the Web.
Specifications
Specification | Status | Comment |
---|---|---|
{{SpecName('HTML WHATWG', "the-canvas-element.html", "Canvas")}} | {{Spec2('HTML WHATWG')}} |