console
/**
* 总结一下,scale()方法会改变图形的以下3点。
* 左上角坐标。
* 宽度或高度。
* 线条宽度。
*/
function $$(id) {
return document.getElementById(id);
}
window.onload = function () {
var cnv = $$("canvas");
var cxt = cnv.getContext("2d");
//绘制初始图形
cxt.fillStyle = "HotPink";
// cxt.fillRect(30, 30, 50, 50);
/**
* cxt.scale(x,y);
* x表示图形在x轴方向的缩放倍数。y表示图形在y轴方向的缩放倍数。其中,x和y一般情况下都是正数。
* 当x或y的取值为0~1时,图形被缩小;当x或y的取值大于1时,图形被放大。
* 其实x和y也可以取负数
*/
//缩放操作
// 在使用scale(1.5, 1.5)之后,图形左上角的坐标值也跟着变为原来的1.5倍了
cxt.scale(1.5, 1.5);
cxt.fillStyle = "LightSkyBlue";
cxt.fillRect(30, 30, 50, 50);
/**
* 每次点击【缩小】或【放大】按钮,图形的缩放是相对于上一次操作之后的图形而言的。
*/
//图形放大
$$("btn-big").onclick = function () {
cxt.scale(1.5, 1.5);
cxt.fillStyle = "#9966FF";
cxt.fillRect(30, 30, 50, 50);
// 回复左上角的坐标
cxt.translate(-15, -15);
}
//图形缩小
$$("btn-small").onclick = function () {
cxt.scale(0.5, 0.5);
cxt.fillStyle = "LightSkyBlue";
cxt.fillRect(30, 30, 50, 50);
}
/**
* scale()除了会改变图形的大小之外,还会改变其他属性,如线条宽度(即lineWidth)、左上角坐标等。
*/
cxt.lineWidth = 4;
cxt.strokeStyle = "HotPink";
// cxt.strokeRect(30, 30, 50, 50);
//图形放大
// $$("btn-big").onclick = function () {
// // 隐藏另一个按钮
// $$("btn-big").style.display='none'
// cxt.scale(1.5, 1.5);
// cxt.strokeRect(30, 30, 50, 50);
// }
}
<canvas id="canvas" width="200" height="150" style="border:1px dashed gray;" class="grid"></canvas>
<br/>
<input id="btn-big" type="button" value="放大" />
<input id="btn-small" type="button" value="缩小" />
/* CSS 多重背景 */
.grid{
background:
-webkit-linear-gradient(top, transparent 9px, blue 10px),
-webkit-linear-gradient(left, transparent 9px, blue 10px)
;
background:
-moz-linear-gradient(top, transparent 9px, blue 10px),
-moz-linear-gradient(left, transparent 9px, blue 10px)
;
background:
-o-linear-gradient(top, transparent 9px, blue 10px),
-o-linear-gradient(left, transparent 9px, blue 10px)
;
background:
-ms-linear-gradient(top, transparent 9px, blue 10px),
-ms-linear-gradient(left, transparent 9px, blue 10px)
;
background:
linear-gradient(top, transparent 9px, blue 10px),
linear-gradient(left, transparent 9px, blue 10px)
;
-webkit-background-size: 10px 10px;
-moz-background-size: 10px 10px;
background-size: 10px 10px;
}