Maison > interface Web > tutoriel HTML > le corps du texte

html5学习笔记:canvas

WBOY
Libérer: 2016-10-15 10:32:00
original
1345 Les gens l'ont consulté

1.什么是canvas?

可以绘制图形的标签。一般用javascript来绘制。

2.创建一个画布

<span style="color: #008080;">1</span> <span style="color: #0000ff;"><span style="color: #ff00ff;">DOCTYPE html</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">2</span> <span style="color: #0000ff;"><span style="color: #800000;">html</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">3</span> <span style="color: #0000ff;"><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">4</span>     <span style="color: #0000ff;"><span style="color: #800000;">title</span><span style="color: #0000ff;">></span><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">5</span> <span style="color: #0000ff;"></span><span style="color: #800000;">head</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">6</span> <span style="color: #0000ff;"><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">7</span>     <span style="color: #0000ff;"><span style="color: #800000;">canvas </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="mycanvas"</span><span style="color: #ff0000;"> width</span><span style="color: #0000ff;">="100"</span><span style="color: #ff0000;"> height</span><span style="color: #0000ff;">="100"</span><span style="color: #0000ff;">></span><span style="color: #800000;">canvas</span><span style="color: #0000ff;">> //创建画布用canvas标签</span>
<span style="color: #008080;">8</span> <span style="color: #0000ff;"></span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span>
<span style="color: #008080;">9</span> <span style="color: #0000ff;"></span><span style="color: #800000;">html</span><span style="color: #0000ff;">></span></span></span></span></span></span></span>
Copier après la connexion

3.在画布上绘图。

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;"><span style="color: #800000;">script </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text/javascript"</span><span style="color: #0000ff;">></span>
<span style="color: #008080;"> 2</span>     <span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;">用javascript来绘制图像</span>
<span style="color: #008080;"> 3</span>     <span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;">获取到canvas元素</span>
<span style="color: #008080;"> 4</span>     <span style="background-color: #f5f5f5; color: #0000ff;">var</span><span style="background-color: #f5f5f5; color: #000000;"> can</span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;">document.getElementById(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">mycanvas</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">);
</span><span style="color: #008080;"> 5</span>     <span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;">创建html5的内置对象,拥有多种绘制路径、矩形、圆形、字符以及添加图像的方法。</span>
<span style="color: #008080;"> 6</span>     <span style="background-color: #f5f5f5; color: #0000ff;">var</span><span style="background-color: #f5f5f5; color: #000000;"> draw</span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;">can.getContext(</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">2d</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">);
</span><span style="color: #008080;"> 7</span>     <span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;">fillStyle();可以是css的颜色,渐变,图案</span>
<span style="color: #008080;"> 8</span> <span style="background-color: #f5f5f5; color: #000000;">    draw.fillStyle</span><span style="background-color: #f5f5f5; color: #000000;">=</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">red</span><span style="background-color: #f5f5f5; color: #000000;">"</span><span style="background-color: #f5f5f5; color: #000000;">;
</span><span style="color: #008080;"> 9</span>     <span style="background-color: #f5f5f5; color: #008000;">//</span><span style="background-color: #f5f5f5; color: #008000;">fillRect(x,y,width,height) 定义填充方式</span>
<span style="color: #008080;">10</span> <span style="background-color: #f5f5f5; color: #000000;">    draw.fillRect(</span><span style="background-color: #f5f5f5; color: #000000;">0</span><span style="background-color: #f5f5f5; color: #000000;">,</span><span style="background-color: #f5f5f5; color: #000000;">0</span><span style="background-color: #f5f5f5; color: #000000;">,</span><span style="background-color: #f5f5f5; color: #000000;">125</span><span style="background-color: #f5f5f5; color: #000000;">,</span><span style="background-color: #f5f5f5; color: #000000;">12</span><span style="background-color: #f5f5f5; color: #000000;">);</span>
<span style="color: #008080;">12</span>     <span style="color: #0000ff;"></span><span style="color: #800000;">script</span><span style="color: #0000ff;">></span></span>
Copier après la connexion

4.canvas-路径

<span style="color: #008080;"> 1</span> <span style="color: #0000ff;">var</span> can=document.getElementById("mycanvas"<span style="color: #000000;">);
</span><span style="color: #008080;"> 2</span>     <span style="color: #0000ff;">var</span> draw=can.getContext("2d"<span style="color: #000000;">);
</span><span style="color: #008080;"> 3</span>     <span style="color: #008000;">//</span><span style="color: #008000;"> 开始一个路径</span>
<span style="color: #008080;"> 4</span> <span style="color: #000000;">    draw.beginPath();
</span><span style="color: #008080;"> 5</span>     <span style="color: #008000;">//</span><span style="color: #008000;"> 设置线的粗细</span>
<span style="color: #008080;"> 6</span>     draw.lineWidth="5"<span style="color: #000000;">;
</span><span style="color: #008080;"> 7</span>     <span style="color: #008000;">//</span><span style="color: #008000;">设置线的颜色</span>
<span style="color: #008080;"> 8</span>     draw.strokeStyle="green"<span style="color: #000000;">;
</span><span style="color: #008080;"> 9</span>     <span style="color: #008000;">//</span><span style="color: #008000;"> 线的起点</span>
<span style="color: #008080;">10</span>     draw.moveTo(0,75<span style="color: #000000;">);
</span><span style="color: #008080;">11</span>     <span style="color: #008000;">//</span><span style="color: #008000;">线的终点 </span>
<span style="color: #008080;">12</span>     draw.lineTo(250,75<span style="color: #000000;">);
</span><span style="color: #008080;">13</span>     <span style="color: #008000;">//</span><span style="color: #008000;"> 开始绘制路径</span>
<span style="color: #008080;">14</span> <span style="color: #000000;">    draw.stroke();
</span><span style="color: #008080;">15</span>     <span style="color: #008000;">//</span><span style="color: #008000;">开始另一个路径</span>
<span style="color: #008080;">16</span> <span style="color: #000000;">    draw.beginPath();
</span><span style="color: #008080;">17</span>     <span style="color: #008000;">//</span><span style="color: #008000;"> 设置线的粗细</span>
<span style="color: #008080;">18</span>     draw.lineWidth="5"<span style="color: #000000;">;
</span><span style="color: #008080;">19</span>     draw.strokeStyle="purple"<span style="color: #000000;">;
</span><span style="color: #008080;">20</span>     draw.moveTo(25,05<span style="color: #000000;">);
</span><span style="color: #008080;">21</span>     draw.lineTo(35,25<span style="color: #000000;">);
</span><span style="color: #008080;">22</span>     draw.stroke();
Copier après la connexion

效果:

 

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal