1、二次和三次贝塞尔曲线
<style>
canvas{
width: 500px;
height: 300px;
border: 1px solid;
}
</style>
</head>
<body>
<canvas id="can" width="500px" height="300px"></canvas>
<script>
var canvas = document.getElementById("can");
var ctx = canvas.getContext("2d");
// 二次贝塞尔曲线
ctx.beginPath();
ctx.moveTo(100,100);
ctx.quadraticCurveTo(200,200,300,100);
ctx.stroke();
// 三次贝塞尔曲线
ctx.beginPath();
ctx.moveTo(100,100);
ctx.quadraticCurveTo(200,200,300,100,400,200);
ctx.stroke();
</script>
</body>
2、模拟波浪:
<style>
canvas{
width: 500px;
height: 300px;
border: 1px solid;
}
</style>
</head>
<body>
<canvas id="can" width="500px" height="300px"></canvas>
<script>
var width = 500;
var height = 300;
var offset = 0;
var num = 0 ;
var canvas = document.getElementById("can");
var ctx = canvas.getContext("2d");
setInterval(function(){
ctx.clearRect(0,0,500,300);
ctx.beginPath();
// 接图型:
ctx.moveTo(0 + offset -500,height / 2);
ctx.quadraticCurveTo(width / 4 + offset-500,height / 2 - Math.sin(num) * 120, width / 2 + offset-500,height / 2);
ctx.quadraticCurveTo(width / 4 * 3 + offset-500,height / 2 + Math.sin(num)* 120, width + offset-500 ,height / 2);
ctx.moveTo(0 + offset,height / 2);
ctx.quadraticCurveTo(width / 4 + offset,height / 2 - Math.sin(num)* 120, width / 2 + offset,height / 2);
ctx.quadraticCurveTo(width / 4 * 3 + offset,height / 2 + Math.sin(num)* 120, width + offset ,height / 2);
ctx.stroke();
offset += 1;
offset %= 500;
num +=0.02;
},1000/30);
</script>
</body>