Home >Web Front-end >H5 Tutorial >HTML5 7 __Canvas: scaling, rotating, creating shadows
1. The canvas API's scale() function is used for scaling.
scale() takes two parameters, representing the two parameters x and y respectively. The value of a dimension. When each parameter displays an image on the canvas, it is passed the amount by which the image should be enlarged (or reduced) on the axis of the direction. If x is 2, it means that all elements in the drawn image will become two. Double width, if the y value is 0.5, all elements of the drawn image will become half the height of the previous one.
For example, the following program can easily create a new tree on canvas.
<!DOCTYPE html> <html> <meta charset="UTF-8"> <title>缩放示例</title> <canvas id="trails" style="border: 1px solid;" width="400" height="600"> </canvas> <script> var gravel = new Image(); gravel.src = "gravel.jpg"; gravel.onload = function () { drawTrails(); } function createCanopyPath(context) { context.beginPath(); context.moveTo(-25, -50); context.lineTo(-10, -80); context.lineTo(-20, -80); context.lineTo(-5, -110); context.lineTo(-15, -110); context.lineTo(0, -140); context.lineTo(15, -110); context.lineTo(5, -110); context.lineTo(20, -80); context.lineTo(10, -80); context.lineTo(25, -50); context.closePath(); } // 将绘制树的方法 放到 drawTree(),方便重用 function drawTree(context) { var trunkGradient = context.createLinearGradient(-5, -50, 5, -50); trunkGradient.addColorStop(0, '#663300'); trunkGradient.addColorStop(0.4, '#996600'); trunkGradient.addColorStop(1, '#552200'); context.fillStyle = trunkGradient; context.fillRect(-5, -50, 10, 50); var canopyShadow = context.createLinearGradient(0, -50, 0, 0); canopyShadow.addColorStop(0, 'rgba(0, 0, 0, 0.5)'); canopyShadow.addColorStop(0.2, 'rgba(0, 0, 0, 0.0)'); context.fillStyle = canopyShadow; context.fillRect(-5, -50, 10, 50); createCanopyPath(context); context.lineWidth = 4; context.lineJoin = 'round'; context.strokeStyle = '#663300'; context.stroke(); context.fillStyle = '#339900'; context.fill(); } function drawTrails() { var canvas = document.getElementById('trails'); var context = canvas.getContext('2d'); // 在 X=130, Y=250的位置绘制第一棵树 context.save(); context.translate(130, 250); drawTree(context); context.restore(); // 在 X=260, Y=500 位置绘制第二棵树 context.save(); context.translate(260, 500); // 将第二棵树的宽高分别放大至原来的2倍 context.scale(2, 2); drawTree(context); context.restore(); context.save(); context.translate(-10, 350); context.beginPath(); context.moveTo(0, 0); context.quadraticCurveTo(170, -50, 260, -190); context.quadraticCurveTo(310, -250, 410,-250); context.strokeStyle = context.createPattern(gravel, 'repeat'); context.lineWidth = 20; context.stroke(); context.restore(); } </script> </html>
2. Rotate context.rotate(angle), The parameter angle is in radians
See the code
<!DOCTYPE html> <html> <meta charset="UTF-8"> <title>旋转示例</title> <canvas id="trails" style="border: 1px solid;" width="400" height="600"> </canvas> <script> var gravel = new Image(); gravel.src = "gravel.jpg"; gravel.onload = function () { drawTrails(); } function createCanopyPath(context) { context.beginPath(); context.moveTo(-25, -50); context.lineTo(-10, -80); context.lineTo(-20, -80); context.lineTo(-5, -110); context.lineTo(-15, -110); context.lineTo(0, -140); context.lineTo(15, -110); context.lineTo(5, -110); context.lineTo(20, -80); context.lineTo(10, -80); context.lineTo(25, -50); context.closePath(); } // 将绘制树的方法 放到 drawTree() function drawTree(context) { var trunkGradient = context.createLinearGradient(-5, -50, 5, -50); trunkGradient.addColorStop(0, '#663300'); trunkGradient.addColorStop(0.4, '#996600'); trunkGradient.addColorStop(1, '#552200'); context.fillStyle = trunkGradient; context.fillRect(-5, -50, 10, 50); var canopyShadow = context.createLinearGradient(0, -50, 0, 0); canopyShadow.addColorStop(0, 'rgba(0, 0, 0, 0.5)'); canopyShadow.addColorStop(0.2, 'rgba(0, 0, 0, 0.0)'); context.fillStyle = canopyShadow; context.fillRect(-5, -50, 10, 50); createCanopyPath(context); context.lineWidth = 4; context.lineJoin = 'round'; context.strokeStyle = '#663300'; context.stroke(); context.fillStyle = '#339900'; context.fill(); } function drawTrails() { var canvas = document.getElementById('trails'); var context = canvas.getContext('2d'); // 在 X=130, Y=250的位置绘制第一棵树 context.save(); context.translate(100, 150); context.rotate(1.57); //旋转角度以弧度为单位 drawTree(context); context.restore(); context.save(); context.translate(-10, 450); context.beginPath(); context.moveTo(0, 0); context.quadraticCurveTo(170, -50, 260, -190); context.quadraticCurveTo(310, -250, 410,-250); context.strokeStyle = context.createPattern(gravel, 'repeat'); context.lineWidth = 20; context.stroke(); context.restore(); } </script> </html>
Things to note about the above two examples:
is always at the origin Perform transformation operations on graphics and paths
The example demonstrates why it is necessary to perform transformation operations on graphics and paths at the origin, and then translate them uniformly after execution. Because of scaling and rotation Transformation operations such as rotate are all performed on the origin.
If you rotate a graphic that is not at the origin, the graphic will rotate around the origin instead of rotating in place. Similarly, if the graphics are not placed at appropriate coordinates when scaling, all coordinates will be scaled at the same time, and the new coordinates may exceed the canvas range.
3. Create a shadow
The following example demonstrates how to freely transform the path coordinates to Fundamentally changes the path display of the tree now, ultimately creating a shadow effect. Related code:
<!DOCTYPE html> <html> <meta charset="UTF-8"> <title>变换阴影</title> <canvas id="trails" style="border: 1px solid;" width="400" height="600"> </canvas> <script> var gravel = new Image(); gravel.src = "gravel.jpg"; gravel.onload = function () { drawTrails(); } function createCanopyPath(context) { context.beginPath(); context.moveTo(-25, -50); context.lineTo(-10, -80); context.lineTo(-20, -80); context.lineTo(-5, -110); context.lineTo(-15, -110); context.lineTo(0, -140); context.lineTo(15, -110); context.lineTo(5, -110); context.lineTo(20, -80); context.lineTo(10, -80); context.lineTo(25, -50); context.closePath(); } function drawTree(context) { context.save(); // X值随着Y值的增加而增加,借助拉伸变换,可以创建一棵用作阴影的倾斜的树,应用了 // 变换以后,所有坐标都与矩阵相乘 context.transform(1, 0, -0.5, 1, 0, 0); // 在Y轴方向,将阴影的高度压缩为原来的60% context.scale(1, 0.6); // 使用透明度为20%的黑色填充树干 context.fillStyle = 'rgba(0, 0, 0, 0.2)'; context.fillRect(-5, -50, 10, 50); // 使用已有的阴影效果重新绘制 createCanopyPath(context); context.fill(); context.restore(); var trunkGradient = context.createLinearGradient(-5, -50, 5, -50); trunkGradient.addColorStop(0, '#663300'); trunkGradient.addColorStop(0.4, '#996600'); trunkGradient.addColorStop(1, '#552200'); context.fillStyle = trunkGradient; context.fillRect(-5, -50, 10, 50); var canopyShadow = context.createLinearGradient(0, -50, 0, 0); canopyShadow.addColorStop(0, 'rgba(0, 0, 0, 0.5)'); canopyShadow.addColorStop(0.2, 'rgba(0, 0, 0, 0.0)'); context.fillStyle = canopyShadow; context.fillRect(-5, -50, 10, 50); createCanopyPath(context); context.lineWidth = 4; context.lineJoin = 'round'; context.strokeStyle = '#663300'; context.stroke(); context.fillStyle = '#339900'; context.fill(); } function drawTrails() { var canvas = document.getElementById('trails'); var context = canvas.getContext('2d'); context.save(); context.translate(130, 250); drawTree(context); context.restore(); context.save(); context.translate(260, 500); context.scale(2, 2); drawTree(context); context.restore(); context.save(); context.translate(-10, 350); context.beginPath(); context.moveTo(0, 0); context.quadraticCurveTo(170, -50, 260, -190); context.quadraticCurveTo(310, -250, 410,-250); context.strokeStyle = context.createPattern(gravel, 'repeat'); context.lineWidth = 20; context.stroke(); context.restore(); } </script> </html>
Analyzing the mathematical meaning behind this transformation, it can be seen that by adjusting the Y-axis value The corresponding parameter changes the value of the X-axis. This is done to stretch out a gray tree for shadow. Next, we reduced the cropped tree to the appropriate size at a 60% ratio.
The clipped "shadow" tree will be displayed first, so that the real tree will be displayed on top of the shadow according to the Z axis (the overlapping order of objects in the canvas) . In addition, the tree shadow is filled using the RGBA feature of CSS. Through the feature, we set the transparency value to 20% under normal circumstances. At this point, the tree shadow with a translucent effect is ready.
The picture used in the examplegravel.jpg can be found in the previous blog post.
The above is HTML5 7 __Canvas: scaling, rotation, and shadow creation. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!