canvas基础
基础画布
<canvas id="c1" width="600" height="600">
当前浏览器版本不支持canvas,请升级浏览器
</canvas>
<script>
let c1 = document.getElementById('c1')
if (!c1.getContext) {
console.warn('当前浏览器版本不支持canvas,请升级浏览器')
}
let ctx = c1.getContext('2d')
console.log('ctx :>> ', ctx)
ctx.fillRect(100, 100, 100, 100)
</script>
填充与路径绘制
<canvas id="c2" width="600" height="600">
当前浏览器版本不支持canvas,请升级浏览器
</canvas>
<script>
var c2 = document.getElementById('c2')
var ctx = c2.getContext('2d')
console.log('ctx :>> ', ctx)
ctx.rect(100, 100, 200, 120)
ctx.stroke()
</script>
矩形绘制-清除模式
<canvas id="c2" width="600" height="600">
当前浏览器版本不支持canvas,请升级浏览器
</canvas>
<script>
var c2 = document.getElementById('c2')
var ctx = c2.getContext('2d')
console.log('ctx :>> ', ctx)
ctx.beginPath()
ctx.rect(200, 150, 200, 100)
ctx.stroke()
ctx.closePath()
ctx.beginPath()
ctx.rect(100, 100, 200, 100)
ctx.fill()
ctx.closePath()
let height = 0
let setV = setInterval(() => {
height++
ctx.clearRect(0, 0, c2.clientWidth, height)
if (height > c2.clientHeight) {
clearInterval(setV)
}
}, 10)
</script>
圆弧
<canvas id="c1" width="600" height="600">
当前浏览器版本不支持canvas,请升级浏览器
</canvas>
<script>
let c1 = document.getElementById('c1')
if (!c1.getContext) {
console.warn('当前浏览器版本不支持canvas,请升级浏览器')
}
let ctx = c1.getContext('2d')
ctx.arc(300, 200, 50, 0, Math.PI * Math.random(), true)
ctx.stroke()
</script>
使用圆弧绘制笑脸
<canvas id="c1" width="600" height="600">
当前浏览器版本不支持canvas,请升级浏览器
</canvas>
<script>
let c1 = document.getElementById('c1')
if (!c1.getContext) {
console.warn('当前浏览器版本不支持canvas,请升级浏览器')
}
let ctx = c1.getContext('2d')
ctx.beginPath()
ctx.arc(75, 75, 50, 0, Math.PI * 2)
ctx.stroke()
ctx.closePath()
ctx.beginPath()
ctx.arc(75, 75, 35, 0, Math.PI)
ctx.stroke()
ctx.closePath()
ctx.beginPath()
ctx.arc(60, 60, 5, 0, Math.PI * 2)
ctx.stroke()
ctx.closePath()
ctx.beginPath()
ctx.arc(90, 60, 5, 0, Math.PI * 2)
ctx.stroke()
ctx.closePath()
</script>
使用moveTo来移动点
<canvas id="c1" width="600" height="600">
当前浏览器版本不支持canvas,请升级浏览器
</canvas>
<script>
let c1 = document.getElementById('c1')
if (!c1.getContext) {
console.warn('当前浏览器版本不支持canvas,请升级浏览器')
}
let ctx = c1.getContext('2d')
ctx.beginPath()
ctx.arc(75, 75, 50, 0, Math.PI * 2)
ctx.moveTo(110, 75)
ctx.arc(75, 75, 35, 0, Math.PI)
ctx.moveTo(65, 65)
ctx.arc(60, 60, 5, 0, Math.PI * 2)
ctx.moveTo(95, 65)
ctx.arc(90, 60, 5, 0, Math.PI * 2)
ctx.stroke()
ctx.closePath()
</script>
绘制线段
<canvas id="c1" width="600" height="600">
当前浏览器版本不支持canvas,请升级浏览器
</canvas>
<script>
let c1 = document.getElementById('c1')
if (!c1.getContext) {
console.warn('当前浏览器版本不支持canvas,请升级浏览器')
}
let ctx = c1.getContext('2d')
ctx.beginPath()
ctx.moveTo(300, 200)
ctx.lineTo(350, 250)
ctx.stroke()
ctx.closePath()
</script>
绘制三角形
<canvas id="c1" width="600" height="600">
当前浏览器版本不支持canvas,请升级浏览器
</canvas>
<script>
let c1 = document.getElementById('c1')
if (!c1.getContext) {
console.warn('当前浏览器版本不支持canvas,请升级浏览器')
}
let ctx = c1.getContext('2d')
ctx.beginPath()
ctx.lineTo(300, 200)
ctx.lineTo(350, 250)
ctx.lineTo(350, 200)
ctx.lineTo(300, 200)
ctx.stroke()
ctx.closePath()
ctx.beginPath()
ctx.lineTo(200, 100)
ctx.lineTo(250, 150)
ctx.lineTo(250, 100)
ctx.lineTo(200, 100)
ctx.fill()
ctx.stroke()
ctx.closePath()
</script>
arcTo绘制圆弧方式
<canvas id="c1" width="600" height="600">
当前浏览器版本不支持canvas,请升级浏览器
</canvas>
<script>
let c1 = document.getElementById('c1')
if (!c1.getContext) {
console.warn('当前浏览器版本不支持canvas,请升级浏览器')
}
let ctx = c1.getContext('2d')
ctx.beginPath()
ctx.moveTo(300, 200)
ctx.arcTo(300, 250, 250, 250, 25)
ctx.stroke()
ctx.closePath()
</script>
二次贝塞尔曲线实现聊天气泡框
<canvas id="c1" width="600" height="600">
当前浏览器版本不支持canvas,请升级浏览器
</canvas>
<script>
let c1 = document.getElementById('c1')
if (!c1.getContext) {
console.warn('当前浏览器版本不支持canvas,请升级浏览器')
}
let ctx = c1.getContext('2d')
ctx.moveTo(200, 300)
ctx.quadraticCurveTo(150, 300, 150, 200)
ctx.quadraticCurveTo(150, 100, 300, 100)
ctx.quadraticCurveTo(450, 100, 450, 200)
ctx.quadraticCurveTo(450, 300, 250, 300)
ctx.quadraticCurveTo(250, 350, 150, 350)
ctx.quadraticCurveTo(200, 350, 200, 300)
ctx.stroke()
</script>
三次贝塞尔曲线实现爱心
<canvas id="c1" width="600" height="600">
当前浏览器版本不支持canvas,请升级浏览器
</canvas>
<script>
let c1 = document.getElementById('c1')
if (!c1.getContext) {
console.warn('当前浏览器版本不支持canvas,请升级浏览器')
}
let ctx = c1.getContext('2d')
ctx.beginPath()
ctx.moveTo(300, 200)
ctx.bezierCurveTo(350, 150, 400, 200, 300, 250)
ctx.bezierCurveTo(200, 200, 250, 150, 300, 200)
ctx.stroke()
ctx.closePath()
</script>
封装路径
<canvas id="c1" width="600" height="600">
当前浏览器版本不支持canvas,请升级浏览器
</canvas>
<script>
let c1 = document.getElementById('c1')
if (!c1.getContext) {
console.warn('当前浏览器版本不支持canvas,请升级浏览器')
}
let ctx = c1.getContext('2d')
var heartPath = new Path2D()
console.log('heartPath :>> ', heartPath)
heartPath.moveTo(300, 200)
heartPath.bezierCurveTo(350, 150, 400, 200, 300, 250)
heartPath.bezierCurveTo(200, 200, 250, 150, 300, 200)
ctx.stroke(heartPath)
ctx.fill(heartPath)
var chatPaht = new Path2D()
chatPaht.moveTo(200, 300)
chatPaht.quadraticCurveTo(150, 300, 150, 200)
chatPaht.quadraticCurveTo(150, 100, 300, 100)
chatPaht.quadraticCurveTo(450, 100, 450, 200)
chatPaht.quadraticCurveTo(450, 300, 250, 300)
chatPaht.quadraticCurveTo(250, 350, 150, 350)
chatPaht.quadraticCurveTo(200, 350, 200, 300)
ctx.stroke(chatPaht)
var polyline = new Path2D('M10 10 h 80 v 80 h -80 z')
ctx.stroke(polyline)
</script>
颜色设置
<canvas id="c1" width="600" height="600">
当前浏览器版本不支持canvas,请升级浏览器
</canvas>
<script>
let c1 = document.getElementById('c1')
if (!c1.getContext) {
console.warn('当前浏览器版本不支持canvas,请升级浏览器')
}
let ctx = c1.getContext('2d')
ctx.globalAlpha = 0.5
var heartPath = new Path2D()
heartPath.moveTo(300, 200)
heartPath.bezierCurveTo(350, 150, 400, 200, 300, 250)
heartPath.bezierCurveTo(200, 200, 250, 150, 300, 200)
ctx.stroke(heartPath)
ctx.fill(heartPath)
var chatPaht = new Path2D()
chatPaht.moveTo(200, 300)
chatPaht.quadraticCurveTo(150, 300, 150, 200)
chatPaht.quadraticCurveTo(150, 100, 300, 100)
chatPaht.quadraticCurveTo(450, 100, 450, 200)
chatPaht.quadraticCurveTo(450, 300, 250, 300)
chatPaht.quadraticCurveTo(250, 350, 150, 350)
chatPaht.quadraticCurveTo(200, 350, 200, 300)
ctx.strokeStyle = 'green'
ctx.strokeStyle = '#ff00ff'
ctx.stroke(chatPaht)
ctx.fillStyle = 'rgba(255, 200, 200, 0.3)'
let polyline = new Path2D('M10 10 h 80 v 80 h -80 z')
ctx.strokeStyle = 'red'
ctx.stroke(polyline)
ctx.fillRect(280, 180, 100, 40)
ctx.fillStyle = '#de4336'
</script>
线性渐变
<canvas id="c1" width="600" height="600">
当前浏览器版本不支持canvas,请升级浏览器
</canvas>
<script>
let c1 = document.getElementById('c1')
if (!c1.getContext) {
console.warn('当前浏览器版本不支持canvas,请升级浏览器')
}
let ctx = c1.getContext('2d')
let index = 0
function render() {
ctx.clearRect(0, 0, 600, 600)
index += 0.005
if (index > 1) {
index = 0
}
let linearGradient = ctx.createLinearGradient(0, 0, 600, 600)
linearGradient.addColorStop(0, 'red')
linearGradient.addColorStop(index, 'orange')
linearGradient.addColorStop(1, 'pink')
ctx.fillStyle = linearGradient
ctx.fillRect(100, 200, 300, 300)
requestAnimationFrame(render)
}
requestAnimationFrame(render)
</script>
径向渐变
<canvas id="c1" width="600" height="600">
当前浏览器版本不支持canvas,请升级浏览器
</canvas>
<script>
let c1 = document.getElementById('c1')
if (!c1.getContext) {
console.warn('当前浏览器版本不支持canvas,请升级浏览器')
}
let ctx = c1.getContext('2d')
let index = 0
let radiaGradinet = ctx.createConicGradient(300, 200, 0, 300, 200, 100)
radiaGradinet.addColorStop(0, 'red')
radiaGradinet.addColorStop(0.3, 'orange')
radiaGradinet.addColorStop(1, 'pink')
ctx.fillStyle = radiaGradinet
ctx.fillRect(0, 0, 600, 600)
</script>