关于canvas图像
var img = new Image(); // Create new Image object
img.src = 'myImage.png'; // Set source path
当脚本执行后,图片开始装载。若调用 drawImage 时,图片没装载完,脚本会等待直至装载完毕。如果不希望这样,可以使用 onload 事件:
var ctx = document.getElementById('myCanvas').getContext('2d');
var img = new Image();
img.src = 'oneeye.jpg';
img.onload = function(){
ctx.drawImage(img,0,0);
}
ctx.drawImage(img,x,y,width,height);
x 在画布上放置图像的 x 坐标位置。
y 在画布上放置图像的 y 坐标位置。width 可选。要使用的图像的宽度。(伸展或缩小图像)
height 可选。要使用的图像的高度。(伸展或缩小图像)
var myImage = document.getElementById("myCanvas");
var cxt = myImage.getContext("2d");
var img = new Image();
img.src = "ji.png";
img.onload = function(){
cxt.drawImage(img, 0, 0, 50, 50);
cxt.drawImage(img, 30, 30, 50, 50);
cxt.drawImage(img, 60, 60, 50, 50);
}
简单的使用
<canvas id="myCanvas" style="width: 1000px; height: 600px; border: 1px solid #c3c3c3"></canvas>
<script type = "text/javascript">
var myImage = document.getElementById("myCanvas");
var cxt = myImage.getContext("2d");
var img = new Image();
img.src = "1.jpg";
img.onload = function(){
cxt.drawImage(img,0,0,200,90);
cxt.drawImage(img,60,30,200,90);
cxt.drawImage(img,90,60,200,90);
}
</script>
这样做呢?
cxt.drawImage(img, 0, 0, 200, 90);
cxt.drawImage(img, 60, 30, 100, 45);
cxt.drawImage(img, 90, 60, 800, 200);
图像的剪贴
var canvas1 = document.getElementById("myCanvas")
var ctx = canvas1.getContext("2d");
var img = new Image();
img.src = 'wode.gif';
img.onload = function(){
ctx.drawImage(img, 10, 10, 30, 30, 50, 50, 100, 100);
}
context.drawImage(img, sx, sy, swidth, sheight, x, y, width, height);
img 规定要使用的图像、画布或视频。
sx 可选。开始剪切的 x 坐标位置。是相对于当前img的左上角,不是原始画布左上角
sy 可选。开始剪切的 y 坐标位置。是相对于当前img的左上角,不是原始画布左上角
swidth 可选。被剪切图像的宽度。
sheight 可选。被剪切图像的高度。
x 在画布上放置图像的 x 坐标位置。
y 在画布上放置图像的 y 坐标位置。
width 可选。要使用的图像的宽度。(伸展或缩小图像)
height 可选。要使用的图像的高度。(伸展或缩小图像)
下面是使用canvas设计的一张海报:
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}
#canvas {
margin: 0 auto;
display: block;
}
#show {
position: fixed;
top: 10px;
left: 10px;
}
</style>
</head>
<body>
<canvas width="1000" height="800" id="canvas" onmousemove="go(event)" onmouseout="out()">
您的浏览器不支持canvas,请升级你的浏览器!
</canvas>
<div id="show"></div>
</body>
<script type="text/javascript">
//获得坐标,便于查找
var show=document.getElementById("show");
document.getElementById("show").innerHTML='ds'
function go(e){
var x=e.clientX;
var y=e.clientY;
document.getElementById("show").innerHTML=x+" "+y;
}
function out(){
show.innerHTML="";
}
//获得canvas对象
var oCanvas = document.getElementById('canvas');
var ctx = oCanvas.getContext('2d');
//背景
ctx.beginPath();
var grd = ctx.createLinearGradient(0, 0, 1000, 0);
grd.addColorStop(0, "#810507");
grd.addColorStop(0.5, "#DA190A");
grd.addColorStop(1, "#810507");
ctx.fillStyle = grd;
ctx.fillRect(0, 0, 1000, 800);
ctx.closePath();
ctx.beginPath();
var grd1 = ctx.createLinearGradient(0, 0, 1000, 0);
grd1.addColorStop(0, "#ff0");
grd1.addColorStop(0.5, "#FA190A");
grd1.addColorStop(1, "#ff0");
ctx.strokeStyle = grd1;
ctx.fillStyle = grd1;
ctx.moveTo(0, 0);
ctx.lineTo(580, 135);
ctx.lineTo(520, 290);
ctx.lineTo(1000, 544);
ctx.lineTo(1000, 800);
ctx.lineTo(402, 326);
ctx.lineTo(490, 177);
ctx.lineTo(0, 20);
ctx.lineTo(0, 0);
ctx.lineWidth = 1;
ctx.fill();
ctx.stroke();
ctx.closePath();
ctx.font = 'bold 120px Microsoft Yahei ';
ctx.fillStyle = '#fff';
ctx.fillText('2-5', 400, 200);
ctx.font = '24px Microsoft Yahei ';
ctx.fillStyle = '#fff';
ctx.fillText('限时', 350, 170);
ctx.font = '24px Microsoft Yahei ';
ctx.fillStyle = '#fff';
ctx.fillText('折扣', 350, 200);
ctx.font = 'bold 54px Microsoft Yahei ';
ctx.fillStyle = '#fff';
ctx.fillText('折', 600, 195);
ctx.beginPath();
ctx.fillStyle = 'orange';
ctx.arc(210, 230, 20, 0, Math.PI * 2, 1);
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.fillStyle = '#FB5707';
ctx.arc(300, 500, 40, 0, Math.PI * 2, 1);
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.fillStyle = '#FA3F08';
ctx.arc(690, 290, 80, 0, Math.PI * 2, 1);
ctx.fill();
ctx.closePath();
ctx.font = 'bold 120px Microsoft Yahei ';
var grd2 = ctx.createLinearGradient(0, 0, 1000, 0);
grd2.addColorStop(0, "#ff0");
grd2.addColorStop(0.5, "#fff");
grd2.addColorStop(1, "#ff0");
ctx.fillStyle = grd2;
ctx.fillText('全场买一送一', 150, 330);
ctx.beginPath();
ctx.strokeStyle = '#fff';
ctx.arc(40, 700, 200, 0, Math.PI * 2, 1);
ctx.lineWidth = 10;
ctx.stroke();
ctx.closePath();
ctx.beginPath();
ctx.strokeStyle = '#ff0';
ctx.arc(300, 800, 400, 0, Math.PI * 2, 1);
ctx.lineWidth = 10;
ctx.stroke();
ctx.closePath();
ctx.beginPath();
ctx.strokeStyle = 'orange';
ctx.arc(900, 0, 200, 0, Math.PI * 2, 1);
ctx.lineWidth = 10;
ctx.stroke();
ctx.closePath();
ctx.beginPath();
ctx.strokeStyle = 'orange';
ctx.arc(650, -30, 100, 0, Math.PI * 2, 1);
ctx.lineWidth = 10;
ctx.stroke();
ctx.closePath();
ctx.beginPath();
ctx.fillStyle = '#ff0';
ctx.arc(10, 50, 4, 0, Math.PI * 2, 1);
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.fillStyle = '#ff0';
ctx.arc(60, 80, 6, 0, Math.PI * 2, 1);
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.fillStyle = 'orange';
ctx.arc(50, 200, 10, 0, Math.PI * 2, 1);
ctx.fill();
ctx.closePath();
ctx.font = 'bold 42px Microsoft Yahei ';
ctx.fillStyle = '#fff';
ctx.fillText('以下书籍更多优惠', 325, 420);
var img1 = new Image();
img1.src = 'images/20640680-1_b.jpg';
img1.onload = function () {
ctx.drawImage(img1, 33, 0, 135, 200, 70, 500, 160, 200);
};
var img2 = new Image();
img2.src = 'images/20664369-1_b.jpg';
img2.onload = function () {
ctx.drawImage(img2, 25, 0, 150, 200, 300, 500, 160, 200);
};
var img3 = new Image();
img3.src = 'images/20670230-1_b.jpg';
img3.onload = function () {
ctx.drawImage(img3, 25, 0, 150, 200, 530, 500, 160, 200);
};
var img4 = new Image();
img4.src = 'images/20679764-1_b.jpg';
img4.onload = function () {
ctx.drawImage(img4, 32, 4, 136, 193, 760, 500, 160, 200);
};
</script>
</html>
效果图:
注意:后画的图层次高于先画的图,可以在代码上调整代码的先后顺序来改变,canvas在网页上是以图片的形式展示,可以直接保存图片