html5如何绘制矩形吗?谈下心得!
2014-03-10 18:33
316 查看
想要创建交互式HTML5游戏之前,让我们先从最基本的开始。
首先,单击“Red Square,如果您没有看到一个画布红场你problably想要正确的web浏览器继续。
<canvas id="c1" width="200" height="200" style="border:solid 1px #000000;"></canvas>
<button onclick="draw_square();return true;">Red Square</button> <script>
function draw_square() {
var c1 = document.getElementById("c1");
var c1_context = c1.getContext("2d");
c1_context.fillStyle = "#f00";
c1_context.fillRect(50, 50, 100, 100);
}
</script>
画在canvas上的任何东西,你不要把任何开始标记和结束标记之间的<canvas>,浏览器支持画布就忽略它。你只能使用Javascript来绘画。
第一条规则,你的canvas元素必须有一个ID,因此我们可以使用Javascript来定位它。其次,每一个canvas都有我们所说的“上下文”。事实上,canvas的背景是我们要油漆的,不是canvas本身。
var c1 = document.getElementById("c1");
var c1_context = c1.getContext("2d");
让我们选择用红漆(fillStyle = " # f00 ";)和油漆100 px宽度和高度的红场。(fillRect(50、50、100、100))。
c1_context.fillStyle = "#f00";
c1_context.fillRect(50, 50, 100, 100);
“上下文”描述的方法
fillstyle css色彩、模式或梯度内的形状。默认fillStyle坚实的黑色。
strokestyle颜色或形状的CSS样式
fillRect(x,y,宽度、高度)画一个矩形从x和y坐标和宽度x高度的大小。
strokeRect(x,y,宽度、高度)画一个矩形,只有它的边缘。
clearRect(x,y,宽度、高度)明确指定的区域在x,y坐标和区域宽度x高度
<div ><canvas id="Canvas2" width="200" height = "200" style="border:solid 1px #000000;"></canvas>
<div>
<button onclick="blue_square_2();return true;">Blue Square</button>
<button onclick="red_stroke_2();return true;">Red Square</button>
<button onclick="clear_rect_2();return true;">Erase Everything</button>
</div>
</div>
<script>
var c2 = document.getElementById("c2");
var c2_context = c2.getContext("2d");
function blue_square_2() { //Blue color square
c2_context.fillStyle = "#00f";
c2_context.fillRect(50, 50, 100, 100);
}
function red_stroke_2() { //Red color edges
c2_context.strokeStyle = "#f00";
c2_context.strokeRect(45, 45, 110, 110);
}
function clear_rect_2() { //Clear all
c2_context.clearRect(40, 40, 120, 120);
}
</script>
首先,单击“Red Square,如果您没有看到一个画布红场你problably想要正确的web浏览器继续。
<canvas id="c1" width="200" height="200" style="border:solid 1px #000000;"></canvas>
<button onclick="draw_square();return true;">Red Square</button> <script>
function draw_square() {
var c1 = document.getElementById("c1");
var c1_context = c1.getContext("2d");
c1_context.fillStyle = "#f00";
c1_context.fillRect(50, 50, 100, 100);
}
</script>
画在canvas上的任何东西,你不要把任何开始标记和结束标记之间的<canvas>,浏览器支持画布就忽略它。你只能使用Javascript来绘画。
第一条规则,你的canvas元素必须有一个ID,因此我们可以使用Javascript来定位它。其次,每一个canvas都有我们所说的“上下文”。事实上,canvas的背景是我们要油漆的,不是canvas本身。
var c1 = document.getElementById("c1");
var c1_context = c1.getContext("2d");
让我们选择用红漆(fillStyle = " # f00 ";)和油漆100 px宽度和高度的红场。(fillRect(50、50、100、100))。
c1_context.fillStyle = "#f00";
c1_context.fillRect(50, 50, 100, 100);
“上下文”描述的方法
fillstyle css色彩、模式或梯度内的形状。默认fillStyle坚实的黑色。
strokestyle颜色或形状的CSS样式
fillRect(x,y,宽度、高度)画一个矩形从x和y坐标和宽度x高度的大小。
strokeRect(x,y,宽度、高度)画一个矩形,只有它的边缘。
clearRect(x,y,宽度、高度)明确指定的区域在x,y坐标和区域宽度x高度
<div ><canvas id="Canvas2" width="200" height = "200" style="border:solid 1px #000000;"></canvas>
<div>
<button onclick="blue_square_2();return true;">Blue Square</button>
<button onclick="red_stroke_2();return true;">Red Square</button>
<button onclick="clear_rect_2();return true;">Erase Everything</button>
</div>
</div>
<script>
var c2 = document.getElementById("c2");
var c2_context = c2.getContext("2d");
function blue_square_2() { //Blue color square
c2_context.fillStyle = "#00f";
c2_context.fillRect(50, 50, 100, 100);
}
function red_stroke_2() { //Red color edges
c2_context.strokeStyle = "#f00";
c2_context.strokeRect(45, 45, 110, 110);
}
function clear_rect_2() { //Clear all
c2_context.clearRect(40, 40, 120, 120);
}
</script>
相关文章推荐
- HTML5如何为形状图上颜色怎么绘制具有颜色和透明度的矩形
- HTML5学习笔记之使用canvas绘制矩形
- HTML5 Canvas实现web画图之自由绘制矩形
- Core Animation - 如何来绘制三个圆角一个直角的矩形
- PhotoShop如何画矩形?PS绘制矩形方法介绍
- html5绘制矩形
- html5-循环方式-绘制矩形
- html5 canvas 绘制圆角矩形
- 如何重写PictureBox的OnPaint方法绘制矩形,并实现拖动,鼠标悬停时提示拖拽方向
- 如何使用GDI绘制半透明矩形
- 【html5每日练习】绘制两个重叠矩形(带滤镜效果)
- HTML5利用Canvas绘制图形(Canvas基本知识、绘制矩形、使用路径和图形组合)
- 如何使用HTML5+CSS3绘制一个QQ 企鹅Logo
- Android 如何使用shape绘制只有一个圆角的矩形
- html5 canvas 详细教程1---样式设置/矩形绘制/路径绘制
- html5 canvas绘制矩形和圆形的实例代码
- html5 canvas 如何清空之前的绘制并重新绘制
- 【实例】html5-canvas绘制线段、矩形和圆
- html5 css3 如何绘制扇形任意角度