canva基礎學習(一)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<canvas id="canvas" style="border: 1px solid #aaa;display: block;margin: 50px auto;">
你當前的瀏覽器不支援canvas,請更換瀏覽器再試。
</canvas>
<script>
window.onload = function(){
var canvas = document.getElementById("canvas");
canvas.width = 800;
canvas.height = 800;
var context = canvas.getContext("2d");
context.lineWidth = 50;
context.strokeStyle = "#005588";
context.beginPath();
context.moveTo(100,200);
context.lineTo(700,200);
context.lineCap = "butt";
context.stroke();
context.beginPath();
context.moveTo(100,400);
context.lineTo(700,400);
context.lineCap = "square";
context.stroke();
context.beginPath();
context.moveTo(100,600);
context.lineTo(700,600);
context.lineCap = "round";
context.stroke();
context.lineWidth = 1;
context.strokeStyle ="#27a";
context.moveTo(100,100);
context.lineTo(100,700);
context.moveTo(700,100);
context.lineTo(700,700);
context.stroke();
}
</script>
</body>
</html>
執行結果如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<canvas id="canvas" style="border: 1px solid #aaa;display: block;margin: 50px auto;">
你當前的瀏覽器不支援canvas,請更換瀏覽器再試。
</canvas>
<script>
window.onload = function(){
var canvas = document.getElementById("canvas");
canvas.height = 800;
canvas.width = 800;
var context = canvas.getContext("2d");
drawRect(context, 100, 100, 400, 400, 10, "#058", "bisque");
drawRect2(context, 300, 300, 400, 400, 10, "#058", "rgba(0,255,0,0.5)");
}
function drawRect(cxt, x, y, width, height, borderWidth, borderColor, fillColor){
cxt.beginPath();
cxt.rect(x, y, width, height);
cxt.closePath();
cxt.lineWidth = borderWidth;
cxt.fillStyle = fillColor;
cxt.strokeStyle = borderColor;
cxt.fill();
cxt.stroke();
}
function drawRect2(cxt, x, y, width, height, borderWidth, borderColor, fillColor){
cxt.lineWidth = borderWidth;
cxt.fillStyle = fillColor;
cxt.strokeStyle = borderColor;
cxt.beginPath();
cxt.fillRect(x, y, width, height);
cxt.beginPath();
cxt.strokeRect(x, y, width, height);
}
</script>
</body>
</html>
執行結果如下:
<span style="font-size:10px;"><!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<canvas id="canvas" style="border: 1px solid #aaa;display: block;margin: 50px auto;">
你當前的瀏覽器不支援canvas,請更換瀏覽器再試。
</canvas>
<script>
window.onload = function(){
var canvas = document.getElementById("canvas");
canvas.width = 800;
canvas.height = 800;
var context = canvas.getContext("2d");
context.lineWidth = 50;
context.strokeStyle = "#005588";
context.beginPath();
context.moveTo(100,200);
context.lineTo(700,200);
context.lineCap = "butt";//這是正常的
context.stroke();
context.beginPath();
context.moveTo(100,400);
context.lineTo(700,400);
context.lineCap = "square";//線條兩端是長方形的
context.stroke();
context.beginPath();
context.moveTo(100,600);
context.lineTo(700,600);
context.lineCap = "round";<span style="font-family:Arial, Helvetica, sans-serif;">//線條兩端是圓弧形的</span></span><span style="font-size:10px;">
context.stroke();
context.lineWidth = 1;
context.strokeStyle ="#27a";
context.moveTo(100,100);
context.lineTo(100,700);
context.moveTo(700,100);
context.lineTo(700,700);
context.stroke();
}
</script>
</body>
</html></span><span style="font-size:18px;">
</span>
執行結果如下:
下面的這種方式畫矩形更加簡單:
以下例子裡定義了一個函式drawStar,裡面的引數都是可以隨意改變的,從而會得到各種各樣的圖形,還有一個引數rot是用來旋轉的,定義圖形選裝的角度:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<canvas id="canvas" style="border: 1px solid #aaa;display: block;margin: 50px auto;">
你當前的瀏覽器不支援canvas,請更換瀏覽器再試。
</canvas>
<script>
window.onload = function(){
var canvas = document.getElementById("canvas");
canvas.width = 800;
canvas.height = 800;
var context = canvas.getContext("2d");
context.lineWidth = 10;
context.strokeStyle = "gold";
drawStar(context, 150,300,400,400,-40);
}
function drawStar(cxt, r, R, x, y, rot){
cxt.beginPath();
for(var i = 0; i < 5; i ++){
cxt.lineTo(Math.cos((18+i*72-rot)/180*Math.PI)*R+x,
-Math.sin((18+i*72-rot)/180*Math.PI)*R+y);
cxt.lineTo(Math.cos((54+i*72-rot)/180*Math.PI)*r+x,
-Math.sin((54+i*72-rot)/180*Math.PI)*r+y)
}
cxt.closePath();
cxt.stroke();
}
</script>
</body>
</html>
執行結果如下:
相關文章
- JAVA學習--JAVA基礎(一)Java
- python基礎學習筆記(一)Python筆記
- 強化學習(一)模型基礎強化學習模型
- JDK學習基礎入門(一)JDK
- Kotlin 基礎學習總結(一)Kotlin
- Python學習之路—Python基礎(一)Python
- Pytest學習(一)- 入門及基礎
- 深入學習Netty(一)NIO基礎篇Netty
- 免殺學習-基礎學習
- Linux基礎學習——檔案基礎Linux
- python基礎學習Python
- Redis 基礎學習Redis
- Linux基礎學習Linux
- Docker 基礎學習Docker
- Flume基礎學習
- 【Vue學習】基礎Vue
- jQuery基礎學習jQuery
- Scala基礎學習
- kafka基礎學習Kafka
- Zookeeper 基礎學習
- 深度學習基礎深度學習
- 學習linux基礎Linux
- Pandas基礎學習
- 【機器學習基礎】神經網路/深度學習基礎機器學習神經網路深度學習
- 零基礎學習人工智慧—Python—Pytorch學習(一)人工智慧PythonPyTorch
- Java 基礎學習系列一 —— Java 主要特性Java
- 密碼學基礎概念 — 密碼學複習(一)密碼學
- Python基礎學習篇Python
- Zookeeper學習——基礎框架框架
- python基礎學習1Python
- linux基礎命令學習Linux
- python基礎學習2Python
- 深度學習--RNN基礎深度學習RNN
- redis學習——基礎指令Redis
- YII-基礎學習
- python基礎學習五Python
- opencv學習之基礎OpenCV
- Spring Boot 學習-基礎Spring Boot
- [pwn基礎]Pwntools學習