您好,欢迎访问一九零五行业门户网

如何使用HTML5创建一个变换矩阵?

in the following article, we are going to learn about how to create a transformation matrix with html5. html5 canvas provides methods that allow modifications directly to the transformation matrix. the transformation matrix must initially be the identity transform. it may then be adjusted using the transformation methods.
using the transform() methodthe transformation matrix of the current context can be changed using the transform() method.in other words,transform() method lets you scale, rotate, move, and skew the current context.
注意− 只有在调用transform()方法之后创建的图形才会受到变换的影响。
语法以下是transform()方法的语法
ctx.transform(m11, m12, m21, m22, dx, dy)
使用set transform()方法the transform(a1, b1, c1, d1, e1, f1) function is then called with the same arguments after the settransform(a1, b1, c1, d1, e1, f1) method resets the current transform to the identity matrix.
语法following is the syntax for set transform() method.
ctx.settransform(m11, m12, m21, m22, dx, dy)
让我们来看一些示例,以更好地理解html5中的变换矩阵
example 1在以下示例中,我们使用transform()方法生成一个矩形。
<!doctype html><html><body> <canvas id=tutorial width=300 height=400></canvas> <script> var canvas = document.getelementbyid(tutorial); if (canvas.getcontext){ var ctx = canvas.getcontext('2d'); ctx.beginpath(); ctx.linewidth = 4; var cos=math.cos(45*math.pi / 180); var sin=math.cos(45*math.pi / 180); ctx.transform(cos, sin, -sin, cos, 160, 20); ctx.strokestyle = green; ctx.strokerect(60, 60, 160, 160); ctx.stroke(); } </script></body></html>
当脚本被执行时,它将通过触发变换方法,在网页上生成一个显示矩形的输出。
example 2在下面的示例中,我们使用了transform()和set transform()方法。
<!doctype html><html><head> <script> function drawshape(){ // get the canvas element using the dom var canvas = document.getelementbyid('mycanvas'); // make sure we don't execute when canvas isn't supported if (canvas.getcontext){ // use getcontext to use the canvas for drawing var ctx = canvas.getcontext('2d'); var sin = math.sin(math.pi/6); var cos = math.cos(math.pi/6); ctx.translate(200, 200); var c = 0; for (var i=0; i <= 12; i++) { c = math.floor(255 / 12 * i); ctx.fillstyle = rgb( + c + , + c + , + c + ); ctx.fillrect(0, 0, 100, 100); ctx.transform(cos, sin, -sin, cos, 0, 0); } ctx.settransform(-1, 0, 0, 1, 200, 200); ctx.fillstyle = rgba(100, 100, 255, 0.5); ctx.fillrect(50, 50, 100, 100); } else { alert('you need safari or firefox 1.5+ to see this demo.'); } } </script></head> <body onload=drawshape();> <canvas id = mycanvas width = 400 height = 400></canvas></body></html>
on running the above script, it will generate an output generated by using transform() and set transform() methods on the webpage.
example 3在下面的示例中,我们正在创建一个数学表达式 σ n = 1。
<!doctype html><html><body onload=tutorial();> <canvas id = mytutorial width = 400 height = 450></canvas> <script> function tutorial() { const ctx = document.getelementbyid('mytutorial').getcontext('2d'); const sin = math.sin(math.pi / 6); const cos = math.cos(math.pi / 6); ctx.translate(100, 100); let c = 0; for (let i = 0; i <= 10; i++) { c = math.floor(255 / 12 * i); ctx.fillstyle = `rgb(88, 214, 141 )`; ctx.fillrect(0, 0, 100, 10); ctx.transform(cos, sin, -sin, cos, 0, 0); } ctx.settransform(-1, 0, 0, 1, 100, 100); ctx.fillstyle = 'rgb(211, 84, 0,0.5 )'; ctx.fillrect(0, 50, 100, 100); } </script></body></html>
当用户尝试执行该脚本时,它将通过在网页上使用transform()和set transform()方法生成的输出进行显示。
以上就是如何使用html5创建一个变换矩阵?的详细内容。
其它类似信息

推荐信息