aboutsummaryrefslogtreecommitdiff
path: root/files/id/web/api/canvas_api/index.html
blob: 0cdd4219fe108429d05df7a61bcd98ed6df4b650 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
---
title: Canvas API
slug: Web/API/Canvas_API
tags:
  - API
  - Canvas
  - Gambar
  - JavaScript
  - Referensi
translation_of: Web/API/Canvas_API
---
<div>{{CanvasSidebar}}</div>

<p class="summary">Ditambahkan di <a href="/en-US/docs/HTML/HTML5">HTML5</a>, <strong>elemen HTML {{HTMLElement("canvas")}} </strong>dapat digunakan untuk menggambar grafik melalui skrip di <a href="/en-US/docs/Web/JavaScript">JavaScript</a>. Sebagai contoh, dapat digunakan untuk menggambar graf, membuat komposisi foto, mencipta animasi, atau bahkan melakukan pemrosesan atau rendering video secara waktu-nyata .</p>

<p>Aplikasi Mozilla mendapat dukungan untuk <code>&lt;canvas&gt;</code> sejak Gecko 1.8 (i.e. <a href="/en-US/docs/Mozilla/Firefox/Releases/1.5">Firefox 1.5</a>). Elemen tersebut semula dikenalkan oleh Apple untuk Dashboard OS X dan Safari. Internet Explorer mendukung <code>&lt;canvas&gt;</code> sejak versi 9 dan seterusnya; untuk versi awal IE, sebuah halaman dapat secara efektif mendukung <code>&lt;canvas&gt;</code> dengan memasukkan skrip dari proyek <a href="https://github.com/arv/explorercanvas">Explorer Canvas</a> Google. Google Chrome dan Opera 9 juga mendukung <code>&lt;canvas&gt;</code>.</p>

<p>Elemen <code>&lt;canvas&gt;</code> juga digunakan oleh <a href="/en-US/docs/Web/WebGL">WebGL</a> untuk menggambar grafik hardware-accelerated 3D di halaman web.</p>

<h2 id="Contoh">Contoh</h2>

<p>Ini hanya cuplikan kode sederhana yang menggunakan metode  {{domxref("CanvasRenderingContext2D.fillRect()")}}.</p>

<h3 id="HTML">HTML</h3>

<pre class="brush: html">&lt;canvas id="canvas"&gt;&lt;/canvas&gt;
</pre>

<h3 id="JavaScript">JavaScript</h3>

<pre class="brush: js">var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');

ctx.fillStyle = 'green';
ctx.fillRect(10, 10, 100, 100);
</pre>

<p>Edit kode di bawah dan lihat perbaruan perubahannya di kanvas secara waktu-nyata:</p>

<div class="hidden">
<h6 id="Playable_code">Playable code</h6>

<pre class="brush: html">&lt;canvas id="canvas" width="400" height="200" class="playable-canvas"&gt;&lt;/canvas&gt;
&lt;div class="playable-buttons"&gt;
  &lt;input id="edit" type="button" value="Edit" /&gt;
  &lt;input id="reset" type="button" value="Reset" /&gt;
&lt;/div&gt;
&lt;textarea id="code" class="playable-code"&gt;
ctx.fillStyle = 'green';
ctx.fillRect(10, 10, 100, 100);&lt;/textarea&gt;
</pre>

<pre class="brush: js">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);
</pre>
</div>

<p>{{ EmbedLiveSample('Playable_code', 700, 360) }}</p>

<h2 id="Reference">Reference</h2>

<div class="index">
<ul>
 <li>{{domxref("HTMLCanvasElement")}}</li>
 <li>{{domxref("CanvasRenderingContext2D")}}</li>
 <li>{{domxref("CanvasGradient")}}</li>
 <li>{{domxref("CanvasImageSource")}}</li>
 <li>{{domxref("CanvasPattern")}}</li>
 <li>{{domxref("ImageBitmap")}}</li>
 <li>{{domxref("ImageData")}}</li>
 <li>{{domxref("RenderingContext")}}</li>
 <li>{{domxref("TextMetrics")}}</li>
 <li>{{domxref("OffscreenCanvas")}}{{experimental_inline}}</li>
 <li>{{domxref("Path2D")}} {{experimental_inline}}{{domxref("ImageBitmapRenderingContext")}}{{experimental_inline}}</li>
</ul>
</div>

<p>The interfaces related to the <code>WebGLRenderingContext</code> are referenced under <a href="/en-US/docs/Web/WebGL" title="/en-US/docs/Web/WebGL">WebGL</a>.</p>

<p>{{domxref("CanvasCaptureMediaStream")}} is related.</p>

<h2 id="Petunjuk_dan_tutorial">Petunjuk dan tutorial</h2>

<dl>
 <dt><a href="/en-US/docs/Web/API/Canvas_API/Tutorial">Tutorial canvas</a></dt>
 <dd>Tutorial komprehensif mengcakup penggunaan sederhana  <code>&lt;canvas&gt;</code> dan fitur lanjutannya.</dd>
 <dt><a href="/en-US/Add-ons/Code_snippets/Canvas">Cuplikan kode: Canvas</a></dt>
 <dd><span id="result_box" lang="id"><span>Beberapa cuplikan kode pengembang berorientasi ekstensi yang melibatkan</span></span> <code>&lt;canvas&gt;</code>.</dd>
 <dt><a href="/en-US/docs/Web/API/Canvas_API/A_basic_ray-caster">Demo: ray-caster sederhana</a></dt>
 <dd>Demo animasi ray-tracing menggunakan canvas.</dd>
 <dt><a href="/en-US/docs/Web/API/Canvas_API/Drawing_DOM_objects_into_a_canvas">Menggambar DOM objects ke canvas</a></dt>
 <dd>Bagaimana menggambar konten DOM, seperti elemen HTML, pada canvas.</dd>
 <dt><a href="/en-US/docs/Web/API/Canvas_API/Manipulating_video_using_canvas">Memanipulasi video dengan canvas</a></dt>
 <dd>Menggabungkan {{HTMLElement("video")}} dan {{HTMLElement("canvas")}} untuk memanipulasi data video secara real time.</dd>
</dl>

<h2 id="Sumber">Sumber</h2>

<h3 id="Generik">Generik</h3>

<ul>
 <li><a href="http://joshondesign.com/p/books/canvasdeepdive/title.html">HTML5 Canvas Deep Dive</a></li>
 <li><a href="http://bucephalus.org/text/CanvasHandbook/CanvasHandbook.html">Canvas Handbook</a></li>
</ul>

<h3 id="Librari">Librari</h3>

<ul>
 <li><a href="http://fabricjs.com">Fabric.js</a> is an open-source canvas library with SVG parsing capabilities.</li>
 <li><a href="https://github.com/ericdrowell/KineticJS">Kinetic.js</a> is an open-source canvas library focused on interactivity for desktop and mobile applications.</li>
 <li><a href="http://paperjs.org/">Paper.js</a> is an open source vector graphics scripting framework that runs on top of the HTML5 Canvas.</li>
 <li><a href="http://origamijs.com/docs/">Origami.js</a> is an open source lightweight canvas library.</li>
 <li><a href="http://libcanvas.github.com/">libCanvas</a> is powerful and lightweight canvas framework.</li>
 <li><a href="https://p5js.org/">p5.js </a>has a full set of canvas drawing functionality for artists, designers, educators and beginners.</li>
 <li><a href="http://processingjs.org">Processing.js</a> is a port of the Processing visualization language.</li>
 <li><a href="https://playcanvas.com/">PlayCanvas</a> is an open source game engine.</li>
 <li><a href="http://www.pixijs.com/">Pixi.js</a> adalah enjine permainan open source.</li>
 <li><a href="http://www.liquidx.net/plotkit/">PlotKit</a> adalah librari charting dan grafik.</li>
 <li><a class="link-https" href="https://github.com/jeremyckahn/rekapi">Rekapi</a> is an animation key-framing API for Canvas.</li>
 <li><a href="http://senchalabs.github.com/philogl/">PhiloGL</a> is a WebGL framework for data visualization, creative coding and game development.</li>
 <li><a href="http://thejit.org/">JavaScript InfoVis Toolkit</a> creates interactive 2D Canvas data visualizations for the Web.</li>
 <li><a href="http://www.createjs.com/easeljs">EaselJS</a> is a free/open source library to make it easier to use canvas for games and art</li>
 <li><a href="http://scrawl.rikweb.org.uk/">Scrawl-canvas</a> is another open-source javascript library for creating and manipulating 2d canvas elements</li>
 <li><a href="https://www.patrick-wied.at/static/heatmapjs/">heatmap.js</a> is an open source library to create canvas based heatmaps</li>
 <li>The <a href="http://zimjs.com">ZIM</a> framework provides conveniences, components and controls for coding creativity on the canvas — includes accessibility and hundreds of colorful tutorials.</li>
</ul>

<h2 id="Spesifikasi">Spesifikasi</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Spesifikasi</th>
   <th scope="col">Status</th>
   <th scope="col">Komentar</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('HTML WHATWG', 'scripting.html#the-canvas-element', '&lt;canvas&gt;')}}</td>
   <td>{{Spec2('HTML WHATWG')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="Lihat_juga">Lihat juga</h2>

<ul>
 <li><a href="/en-US/docs/Web/WebGL">WebGL</a></li>
</ul>