html5 canvas point,HTML5 Canvas - Translation

HTML5 Canvas - Translation

Advertisements

HTML5 canvas provides translate(x, y) method which is used to move the canvas and its origin to a different point in the grid.

Here argument x is the amount the canvas is moved to the left or right, and y is the amount it's moved up or down.

Example

Following is a simple example which makes use of above method to draw various Spirographs −

#test {

width:100px;

height:100px;

margin:0px auto;

}

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');

ctx.fillRect(0,0,300,300);

for (i = 0;i<3;i++) {

for (j = 0;j<3;j++) {

ctx.save();

ctx.strokeStyle = "#FF0066";

ctx.translate(50+j*100,50+i*100);

drawSpirograph(ctx,10*(j+3)/(j+2),-2*(i+3)/(i+1),10);

ctx.restore();

}

}

} else {

alert('You need Safari or Firefox 1.5+ to see this demo.');

}

}

function drawSpirograph(ctx,R,r,O) {

var x1 = R-O;

var y1 = 0;

var i = 1;

ctx.beginPath();

ctx.moveTo(x1,y1);

do {

if (i>20000) break;

var x2 = (R+r)*Math.cos(i*Math.PI/72) - (r+O)*Math.cos(((R+r)/r)*(i*Math.PI/72))

var y2 = (R+r)*Math.sin(i*Math.PI/72) - (r+O)*Math.sin(((R+r)/r)*(i*Math.PI/72))

ctx.lineTo(x2,y2);

x1 = x2;

y1 = y2;

i++;

} while (x2 != R-O && y2 != 0 );

ctx.stroke();

}

The above example would produce the following result −

html5_canvas.htm

Advertisements

import matplotlib.pyplot as plt import numpy as np from matplotlib.backend_bases import MouseEvent from matplotlib.backend_bases import KeyEvent from matplotlib.lines import Line2D import random class Point: """ 定义2D点 """ def __init__(self, x, y): self.x = x self.y = y # 该点的向量形式 pass def update(self): """ 向量和顶点坐标的同步方法: 由于运算用向量,绘制用点坐标,所以每次运算完,应该调用该方法同步一下坐标和向量值 """ pass def __str__(self): return f"({self.x}, {self.y})" class Stars5: """ 定义五角星图形 """ def __init__(self, points): # 原始顶点,用于还原图形 self.orig_ps = points.copy() # 顶点向量 self.points = points.copy() # 四向平移矩阵 pass # 缩放矩阵 # 反射矩阵 # 旋转矩阵, 正向旋转1度和逆向旋转1度 def reset(self, event): """重置图案""" self.points = self.orig_ps.copy() self.draw(event) def draw(self, event=None): """绘制图案""" xs, ys = [], [] for i in self.points: xs.append(i.x) ys.append(i.y) xs.append(self.points[0].x) ys.append(self.points[0].y) plt.plot(xs, ys, c='b') if event is not None: plt.pause(0.001) event.canvas.draw_idle() def translation(self, event, direct=1): """平移变换""" # 根据方向,做四向平移 pass # 通过向量更新顶点,用于更新绘图 pass # 更新绘图 pass def rotation(self, event, direct=5): """旋转变换""" pass def scale(self, event, direct=7): """缩放变换""" pass def reflection(self, event, direct=9): """反射变换""" pass def __str__(self): result = "五角星\n" for i in self.points: result += f"顶点:{i}\n" return result # 坐标轴大小 xlim = [-400, 400] ylim = [-400, 400] # 五角星外五边形顶点 r = 100 pi_val = np.pi / 180 A = Point(0, r) B = Point(r * np.cos(18 * pi_val), r * np.sin(18 * pi_val)) C = Point(r * np.cos(54 * pi_val), - r * np.sin(54 * pi_val)) D = Point(- r * np.cos(54 * pi_val), - r * np.sin(54 * pi_val)) E = Point(- r * np.cos(18 * pi_val), r * np.sin(18 * pi_val)) points = [A, C, E, B, D] star = Stars5(points) print(star) def draw_axis(xlim, ylim, grid_on=True): plt.cla() ax = plt.gca() # 获取当前坐标轴 ax.set_aspect('equal') # 保持x和y轴比例相同 plt.xlim(xlim) plt.ylim(ylim) # 开启网格线 plt.grid(grid_on) def on_click(event): if event.inaxes and event.button == 3: # 确保点击发生在坐标轴区域内,鼠标右键点击 print("点击右键,复原图形") pass def on_key(event): print(f"按下了{event.key}键") pass # 设置画布大小 fig = plt.figure(figsize=(6, 6)) # 绑定事件 fig.canvas.mpl_connect('button_press_event', on_click) fig.canvas.mpl_connect('key_press_event', on_key) draw_axis(xlim, ylim) star.draw() plt.show()
最新发布
11-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值