<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>多彩画布</title>
<style>
body {
margin:0;
overflow: hidden;
}
#canvas {
background-color: pink;
}
</style>
</head>
<body >
<!-- 建立canvas画布 css的颜色不能和图形交互-->
<canvas id="canvas"></canvas>
<script type='module'>
import { Color } from 'https://unpkg.com/three/build/three.module.js'
// 获取canvas画布
const canvas = document.getElementById('canvas')
// 设置尺寸
canvas.width = window.innerWidth
canvas.height = window.innerHeight
// 使用canvas获取webgl绘图上下文
/** 2维画笔
* const gl = canvas.getContext('2d')
**/
// 3维画笔
const gl = canvas.getContext('webgl')
// 指定将要用来清空绘图区颜色(声明颜色 rgba) 颜色对应 0-1[0,1] 1相当于css[0,255] 255
gl.clearColor(0, 0, 0, 1)
// 刷底色 颜色缓冲区
gl.clear(gl.COLOR_BUFFER_BIT)
// 实例化Color对象
const color = new Color('rgba(255,0,0,1)')
// 解构r g b
const {r,g,b} = color
// 声明颜色 rgb
gl.clearColor(r,g,b, 1)
// 刷底色 颜色缓冲区
gl.clear(gl.COLOR_BUFFER_BIT)
!(function animation() {
// HSL色相 饱和度 亮度 颜色偏移
color.offsetHSL(0.005, 0, 0)
const {r,g,b} = color
gl.clearColor(r,g,b, 1)
// 刷底色 颜色缓冲区
gl.clear(gl.COLOR_BUFFER_BIT)
requestAnimationFrame(animation)
})()
</script>
</body>
</html>
webgl多彩画布
于 2021-04-21 11:23:27 首次发布