作业一:掌握角度与弧度之前换算的公式
作业二:1、画一个矩形作为原有图形 【作为第一个图形】
2、画一个圆作为第二个图形
3、这2个图形进行遮盖,使用5个典型的遮盖属性值,体现具体效果。
备注:作业二要求贴图。
<style>
body{ padding-top:20px;}
canvas{ float:left;}
</style>
</head>
<body>
<canvas id="myCanvas1" width="300" height="400"></canvas>
<canvas id="myCanvas2" width="300" height="400"></canvas>
<canvas id="myCanvas3" width="300" height="400"></canvas>
<canvas id="myCanvas4" width="300" height="400"></canvas>
<canvas id="myCanvas5" width="300" height="400"></canvas>
<canvas id="myCanvas6" width="300" height="400"></canvas>
<canvas id="myCanvas7" width="300" height="400"></canvas>
<canvas id="myCanvas8" width="300" height="400"></canvas>
<canvas id="myCanvas9" width="300" height="400"></canvas>
</body>
<script>
var idN=1;
cover("")
cover("source-over")
cover("destination-over");
cover("source-in")
cover("destination-in");
cover("source-out")
cover("destination-out")
cover("source-atop")
cover("destination-atop")
function cover(type){
var ctx=document.getElementById('myCanvas'+idN++).getContext('2d');
ctx.save();
ctx.fillStyle="blue";
ctx.fillRect(0,0,200,200);
ctx.translate(50,50)
ctx.rotate(0.5);
ctx.fillStyle="yellow";
ctx.globalCompositeOperation=type
ctx.fillRect(0,0,180,180)
ctx.restore();
ctx.fillStyle="crimson";
ctx.font="28px Arial";
if(type=="")type="默认(source-over)"
ctx.fillText(type,10,320)
}
</script>
好厉害
<!--开始--><!--作业一:换算公式为ctx.rotate(20*Math.PI/180);--><!DOCTYPE html><html>
<head> <meta charset="UTF-8"> <title>js练习</title> <style type="text/css"> canvas { height: 500; width: 500; margin: 5px; } </style> </head> <body> <p> <canvas id="c-one" width="300" height="300"></canvas> <canvas id="c-two" width="300" height="300"></canvas> <canvas id="c-three" width="300" height="300"></canvas> </p> <p> <canvas id="c-four" width="300" height="300"></canvas> <canvas id="c-five" width="300" height="300"></canvas> <canvas id="c-six" width="300" height="300"></canvas> </p> <p> <canvas id="c-seven" width="300" height="300"></canvas> </p> <script type="text/javascript">//作业二:1.画矩形 var ctx = document.getElementById("c-one").getContext("2d"); ctx.translate(100,150); ctx.strokeRect(0, 0, 150, 100); ctx.fillStyle = "palevioletred"; ctx.fillRect(0, 0, 150, 100);//作业二:2.加入圆形和旋转 var ctx = document.getElementById("c-two").getContext("2d"); ctx.translate(100,100); ctx.rotate(15*Math.PI/180); ctx.strokeRect(0, 0, 150, 100); ctx.fillStyle = "palevioletred"; ctx.fillRect(0, 0, 150, 100);// ctx.beginPath(); ctx.arc(80,90,70,0,2*Math.PI,false); ctx.closePath(); ctx.fillStyle = "mediumpurple"; ctx.fill();//作业二:3.五种样式//(1) var ctx = document.getElementById("c-three").getContext("2d"); ctx.translate(100,100); ctx.rotate(15*Math.PI/180); ctx.strokeRect(0, 0, 150, 100); ctx.fillStyle = "palevioletred"; ctx.fillRect(0, 0, 150, 100);// ctx.globalCompositeOperation = "destination-out";// ctx.beginPath(); ctx.arc(80,90,70,0,2*Math.PI,false); ctx.closePath(); ctx.fillStyle = "mediumpurple"; ctx.fill();//(2) var ctx = document.getElementById("c-four").getContext("2d"); ctx.translate(100,100); ctx.rotate(15*Math.PI/180); ctx.strokeRect(0, 0, 150, 100); ctx.fillStyle = "palevioletred"; ctx.fillRect(0, 0, 150, 100);// ctx.globalCompositeOperation = "source-atop";// ctx.beginPath(); ctx.arc(80,90,70,0,2*Math.PI,false); ctx.closePath(); ctx.fillStyle = "mediumpurple"; ctx.fill();//(3) var ctx = document.getElementById("c-five").getContext("2d"); ctx.translate(100,100); ctx.rotate(15*Math.PI/180); ctx.strokeRect(0, 0, 150, 100); ctx.fillStyle = "palevioletred"; ctx.fillRect(0, 0, 150, 100);// ctx.globalCompositeOperation = "destination-over";// ctx.beginPath(); ctx.arc(80,90,70,0,2*Math.PI,false); ctx.closePath(); ctx.fillStyle = "mediumpurple"; ctx.fill();//(4) var ctx = document.getElementById("c-six").getContext("2d"); ctx.translate(100,100); ctx.rotate(15*Math.PI/180); ctx.strokeRect(0, 0, 150, 100); ctx.fillStyle = "palevioletred"; ctx.fillRect(0, 0, 150, 100);// ctx.globalCompositeOperation = "xor";// ctx.beginPath(); ctx.arc(80,90,70,0,2*Math.PI,false); ctx.closePath(); ctx.fillStyle = "mediumpurple"; ctx.fill();//(5) var ctx = document.getElementById("c-seven").getContext("2d"); ctx.translate(100,100); ctx.rotate(15*Math.PI/180); ctx.strokeRect(0, 0, 150, 100); ctx.fillStyle = "palevioletred"; ctx.fillRect(0, 0, 150, 100);// ctx.globalCompositeOperation = "source-out";// ctx.beginPath(); ctx.arc(80,90,70,0,2*Math.PI,false); ctx.closePath(); ctx.fillStyle = "mediumpurple"; ctx.fill(); </script> </body></html><!--结束-->
function compFun() {
var ctx = document.getElementById("myC1").getContext("2d");
ctx.fillStyle = "blueviolet";
ctx.fillRect(20, 20, 80, 60);
ctx.globalCompositeOperation = "source-over";
ctx.fillStyle = "red";
ctx.arc(50, 30, 30, 0, 2 * Math.PI, false);
ctx.fill();
var ctx = document.getElementById("myC2").getContext("2d");
ctx.globalCompositeOperation = "destination-over";
var ctx = document.getElementById("myC3").getContext("2d");
ctx.globalCompositeOperation = "source-in";
var ctx = document.getElementById("myC4").getContext("2d");
ctx.globalCompositeOperation = "destination-in";
var ctx = document.getElementById("myC5").getContext("2d");
ctx.globalCompositeOperation = "source-out";
var ctx = document.getElementById("myC6").getContext("2d");
ctx.globalCompositeOperation = "destination-out";
var ctx = document.getElementById("myC7").getContext("2d");
ctx.globalCompositeOperation = "source-atop";
var ctx = document.getElementById("myC8").getContext("2d");
ctx.globalCompositeOperation = "destination-atop";
var ctx = document.getElementById("myC9").getContext("2d");
ctx.globalCompositeOperation = "copy";
var ctx = document.getElementById("myC10").getContext("2d");
ctx.globalCompositeOperation = "xor";
var ctx = document.getElementById("myC11").getContext("2d");
ctx.globalCompositeOperation = "lighter";
var ctx = document.getElementById("myC12").getContext("2d");
ctx.globalCompositeOperation = "darker";
ctx.shadowColor="blue";
ctx.shadowBlur=2;
ctx.shadowOffsetX=5;
ctx.shadowOffsetY=3
ctx.font="10px Arial"
ctx.fillStyle="chocolate"
ctx.fillText("darker",20,90);
我只能写成这样的,簇簇那样的写总是报错。
<!--作业一:换算公式为ctx.rotate(20*Math.PI/180);-->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>2016.4.19 JavaScript练习</title>
<style type="text/css">
canvas {
margin: 5px;
<p>
<canvas id="c-one" width="300" height="300"></canvas>
<canvas id="c-two" width="300" height="300"></canvas>
<canvas id="c-three" width="300" height="300"></canvas>
</p>
<canvas id="c-four" width="300" height="300"></canvas>
<canvas id="c-five" width="300" height="300"></canvas>
<canvas id="c-six" width="300" height="300"></canvas>
<canvas id="c-seven" width="300" height="300"></canvas>
<script type="text/javascript">
// 作业二:1.画矩形
function drawRec(recId, recCon) {
var ctx = document.getElementById(recId).getContext(recCon);
ctx.translate(100,150);
ctx.strokeRect(0, 0, 150, 100);
ctx.fillStyle = "palevioletred";
ctx.fillRect(0, 0, 150, 100);
drawRec("c-one", "2d");
// 作业二:2-1.定义矩形,圆形的旋转,遮盖函数
function drawGrp(grpId, grpCon, grpZG) {
var ctx = document.getElementById(grpId).getContext(grpCon);
ctx.translate(100,100);
ctx.rotate(15*Math.PI/180);
ctx.globalCompositeOperation = grpZG;
ctx.beginPath();
ctx.arc(80,90,70,0,2*Math.PI,false);
ctx.closePath();
ctx.fillStyle = "mediumpurple";
// 作业二:2-2.原始状态
drawGrp("c-two", "2d");
// 作业二:3.五种样式
drawGrp("c-three", "2d", "destination-out");
drawGrp("c-four", "2d", "source-atop");
drawGrp("c-five", "2d", "destination-over");
drawGrp("c-six", "2d", "xor");
drawGrp("c-seven", "2d","source-out");
</html>
效果跟上边完全一致。
<style>
body{ padding-top:20px;}
canvas{ float:left;}
</style>
</head>
<body>
<canvas id="myCanvas1" width="300" height="400"></canvas>
<canvas id="myCanvas2" width="300" height="400"></canvas>
<canvas id="myCanvas3" width="300" height="400"></canvas>
<canvas id="myCanvas4" width="300" height="400"></canvas>
<canvas id="myCanvas5" width="300" height="400"></canvas>
<canvas id="myCanvas6" width="300" height="400"></canvas>
<canvas id="myCanvas7" width="300" height="400"></canvas>
<canvas id="myCanvas8" width="300" height="400"></canvas>
<canvas id="myCanvas9" width="300" height="400"></canvas>
</body>
<script>
var idN=1;
cover("")
cover("source-over")
cover("destination-over");
cover("source-in")
cover("destination-in");
cover("source-out")
cover("destination-out")
cover("source-atop")
cover("destination-atop")
function cover(type){
var ctx=document.getElementById('myCanvas'+idN++).getContext('2d');
ctx.save();
ctx.fillStyle="blue";
ctx.fillRect(0,0,200,200);
ctx.translate(50,50)
ctx.rotate(0.5);
ctx.fillStyle="yellow";
ctx.globalCompositeOperation=type
ctx.fillRect(0,0,180,180)
ctx.restore();
ctx.fillStyle="crimson";
ctx.font="28px Arial";
if(type=="")type="默认(source-over)"
ctx.fillText(type,10,320)
}
</script>
好厉害
<!--开始-->
<!--
作业一:换算公式为ctx.rotate(20*Math.PI/180);
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js练习</title>
<style type="text/css">
canvas {
height: 500;
width: 500;
margin: 5px;
}
</style>
</head>
<body>
<p>
<canvas id="c-one" width="300" height="300"></canvas>
<canvas id="c-two" width="300" height="300"></canvas>
<canvas id="c-three" width="300" height="300"></canvas>
</p>
<p>
<canvas id="c-four" width="300" height="300"></canvas>
<canvas id="c-five" width="300" height="300"></canvas>
<canvas id="c-six" width="300" height="300"></canvas>
</p>
<p>
<canvas id="c-seven" width="300" height="300"></canvas>
</p>
<script type="text/javascript">
//作业二:1.画矩形
var ctx = document.getElementById("c-one").getContext("2d");
ctx.translate(100,150);
ctx.strokeRect(0, 0, 150, 100);
ctx.fillStyle = "palevioletred";
ctx.fillRect(0, 0, 150, 100);
//作业二:2.加入圆形和旋转
var ctx = document.getElementById("c-two").getContext("2d");
ctx.translate(100,100);
ctx.rotate(15*Math.PI/180);
ctx.strokeRect(0, 0, 150, 100);
ctx.fillStyle = "palevioletred";
ctx.fillRect(0, 0, 150, 100);
//
ctx.beginPath();
ctx.arc(80,90,70,0,2*Math.PI,false);
ctx.closePath();
ctx.fillStyle = "mediumpurple";
ctx.fill();
//作业二:3.五种样式
//(1)
var ctx = document.getElementById("c-three").getContext("2d");
ctx.translate(100,100);
ctx.rotate(15*Math.PI/180);
ctx.strokeRect(0, 0, 150, 100);
ctx.fillStyle = "palevioletred";
ctx.fillRect(0, 0, 150, 100);
//
ctx.globalCompositeOperation = "destination-out";
//
ctx.beginPath();
ctx.arc(80,90,70,0,2*Math.PI,false);
ctx.closePath();
ctx.fillStyle = "mediumpurple";
ctx.fill();
//(2)
var ctx = document.getElementById("c-four").getContext("2d");
ctx.translate(100,100);
ctx.rotate(15*Math.PI/180);
ctx.strokeRect(0, 0, 150, 100);
ctx.fillStyle = "palevioletred";
ctx.fillRect(0, 0, 150, 100);
//
ctx.globalCompositeOperation = "source-atop";
//
ctx.beginPath();
ctx.arc(80,90,70,0,2*Math.PI,false);
ctx.closePath();
ctx.fillStyle = "mediumpurple";
ctx.fill();
//(3)
var ctx = document.getElementById("c-five").getContext("2d");
ctx.translate(100,100);
ctx.rotate(15*Math.PI/180);
ctx.strokeRect(0, 0, 150, 100);
ctx.fillStyle = "palevioletred";
ctx.fillRect(0, 0, 150, 100);
//
ctx.globalCompositeOperation = "destination-over";
//
ctx.beginPath();
ctx.arc(80,90,70,0,2*Math.PI,false);
ctx.closePath();
ctx.fillStyle = "mediumpurple";
ctx.fill();
//(4)
var ctx = document.getElementById("c-six").getContext("2d");
ctx.translate(100,100);
ctx.rotate(15*Math.PI/180);
ctx.strokeRect(0, 0, 150, 100);
ctx.fillStyle = "palevioletred";
ctx.fillRect(0, 0, 150, 100);
//
ctx.globalCompositeOperation = "xor";
//
ctx.beginPath();
ctx.arc(80,90,70,0,2*Math.PI,false);
ctx.closePath();
ctx.fillStyle = "mediumpurple";
ctx.fill();
//(5)
var ctx = document.getElementById("c-seven").getContext("2d");
ctx.translate(100,100);
ctx.rotate(15*Math.PI/180);
ctx.strokeRect(0, 0, 150, 100);
ctx.fillStyle = "palevioletred";
ctx.fillRect(0, 0, 150, 100);
//
ctx.globalCompositeOperation = "source-out";
//
ctx.beginPath();
ctx.arc(80,90,70,0,2*Math.PI,false);
ctx.closePath();
ctx.fillStyle = "mediumpurple";
ctx.fill();
</script>
</body>
</html>
<!--结束-->
function compFun() {
var ctx = document.getElementById("myC1").getContext("2d");
ctx.fillStyle = "blueviolet";
ctx.fillRect(20, 20, 80, 60);
ctx.globalCompositeOperation = "source-over";
ctx.fillStyle = "red";
ctx.arc(50, 30, 30, 0, 2 * Math.PI, false);
ctx.fill();
var ctx = document.getElementById("myC2").getContext("2d");
ctx.fillStyle = "blueviolet";
ctx.fillRect(20, 20, 80, 60);
ctx.globalCompositeOperation = "destination-over";
ctx.fillStyle = "red";
ctx.arc(50, 30, 30, 0, 2 * Math.PI, false);
ctx.fill();
var ctx = document.getElementById("myC3").getContext("2d");
ctx.fillStyle = "blueviolet";
ctx.fillRect(20, 20, 80, 60);
ctx.globalCompositeOperation = "source-in";
ctx.fillStyle = "red";
ctx.arc(50, 30, 30, 0, 2 * Math.PI, false);
ctx.fill();
var ctx = document.getElementById("myC4").getContext("2d");
ctx.fillStyle = "blueviolet";
ctx.fillRect(20, 20, 80, 60);
ctx.globalCompositeOperation = "destination-in";
ctx.fillStyle = "red";
ctx.arc(50, 30, 30, 0, 2 * Math.PI, false);
ctx.fill();
var ctx = document.getElementById("myC5").getContext("2d");
ctx.fillStyle = "blueviolet";
ctx.fillRect(20, 20, 80, 60);
ctx.globalCompositeOperation = "source-out";
ctx.fillStyle = "red";
ctx.arc(50, 30, 30, 0, 2 * Math.PI, false);
ctx.fill();
var ctx = document.getElementById("myC6").getContext("2d");
ctx.fillStyle = "blueviolet";
ctx.fillRect(20, 20, 80, 60);
ctx.globalCompositeOperation = "destination-out";
ctx.fillStyle = "red";
ctx.arc(50, 30, 30, 0, 2 * Math.PI, false);
ctx.fill();
var ctx = document.getElementById("myC7").getContext("2d");
ctx.fillStyle = "blueviolet";
ctx.fillRect(20, 20, 80, 60);
ctx.globalCompositeOperation = "source-atop";
ctx.fillStyle = "red";
ctx.arc(50, 30, 30, 0, 2 * Math.PI, false);
ctx.fill();
var ctx = document.getElementById("myC8").getContext("2d");
ctx.fillStyle = "blueviolet";
ctx.fillRect(20, 20, 80, 60);
ctx.globalCompositeOperation = "destination-atop";
ctx.fillStyle = "red";
ctx.arc(50, 30, 30, 0, 2 * Math.PI, false);
ctx.fill();
var ctx = document.getElementById("myC9").getContext("2d");
ctx.fillStyle = "blueviolet";
ctx.fillRect(20, 20, 80, 60);
ctx.globalCompositeOperation = "copy";
ctx.fillStyle = "red";
ctx.arc(50, 30, 30, 0, 2 * Math.PI, false);
ctx.fill();
var ctx = document.getElementById("myC10").getContext("2d");
ctx.fillStyle = "blueviolet";
ctx.fillRect(20, 20, 80, 60);
ctx.globalCompositeOperation = "xor";
ctx.fillStyle = "red";
ctx.arc(50, 30, 30, 0, 2 * Math.PI, false);
ctx.fill();
var ctx = document.getElementById("myC11").getContext("2d");
ctx.fillStyle = "blueviolet";
ctx.fillRect(20, 20, 80, 60);
ctx.globalCompositeOperation = "lighter";
ctx.fillStyle = "red";
ctx.arc(50, 30, 30, 0, 2 * Math.PI, false);
ctx.fill();
var ctx = document.getElementById("myC12").getContext("2d");
ctx.fillStyle = "blueviolet";
ctx.fillRect(20, 20, 80, 60);
ctx.globalCompositeOperation = "darker";
ctx.fillStyle = "red";
ctx.arc(50, 30, 30, 0, 2 * Math.PI, false);
ctx.fill();
ctx.shadowColor="blue";
ctx.shadowBlur=2;
ctx.shadowOffsetX=5;
ctx.shadowOffsetY=3
ctx.font="10px Arial"
ctx.fillStyle="chocolate"
ctx.fillText("darker",20,90);
}
我只能写成这样的,簇簇那样的写总是报错。
<!--作业一:换算公式为ctx.rotate(20*Math.PI/180);-->
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>2016.4.19 JavaScript练习</title>
<style type="text/css">
canvas {
margin: 5px;
}
</style>
</head>
<body>
<p>
<canvas id="c-one" width="300" height="300"></canvas>
<canvas id="c-two" width="300" height="300"></canvas>
<canvas id="c-three" width="300" height="300"></canvas>
</p>
<p>
<canvas id="c-four" width="300" height="300"></canvas>
<canvas id="c-five" width="300" height="300"></canvas>
<canvas id="c-six" width="300" height="300"></canvas>
</p>
<p>
<canvas id="c-seven" width="300" height="300"></canvas>
</p>
<script type="text/javascript">
// 作业二:1.画矩形
function drawRec(recId, recCon) {
var ctx = document.getElementById(recId).getContext(recCon);
ctx.translate(100,150);
ctx.strokeRect(0, 0, 150, 100);
ctx.fillStyle = "palevioletred";
ctx.fillRect(0, 0, 150, 100);
}
drawRec("c-one", "2d");
// 作业二:2-1.定义矩形,圆形的旋转,遮盖函数
function drawGrp(grpId, grpCon, grpZG) {
var ctx = document.getElementById(grpId).getContext(grpCon);
ctx.translate(100,100);
ctx.rotate(15*Math.PI/180);
ctx.strokeRect(0, 0, 150, 100);
ctx.fillStyle = "palevioletred";
ctx.fillRect(0, 0, 150, 100);
ctx.globalCompositeOperation = grpZG;
ctx.beginPath();
ctx.arc(80,90,70,0,2*Math.PI,false);
ctx.closePath();
ctx.fillStyle = "mediumpurple";
ctx.fill();
}
// 作业二:2-2.原始状态
drawGrp("c-two", "2d");
// 作业二:3.五种样式
drawGrp("c-three", "2d", "destination-out");
drawGrp("c-four", "2d", "source-atop");
drawGrp("c-five", "2d", "destination-over");
drawGrp("c-six", "2d", "xor");
drawGrp("c-seven", "2d","source-out");
</script>
</body>
</html>
效果跟上边完全一致。