image2d
Version:
🍇 使用ECMAScript绘制二维图片。Drawing Two-Dimensional Pictures Using ECMAScript.
80 lines (67 loc) • 2.07 kB
JavaScript
/**
* 点(x,y)围绕中心(cx,cy)旋转deg度
*/
export let rotate = function (cx, cy, deg, x, y) {
var cos = Math.cos(deg), sin = Math.sin(deg);
return [
+((x - cx) * cos - (y - cy) * sin + cx).toFixed(7),
+((x - cx) * sin + (y - cy) * cos + cy).toFixed(7)
];
};
/**
* 点(x,y)沿着向量(ax,ay)方向移动距离d
*/
export let move = function (ax, ay, d, x, y) {
var sqrt = Math.sqrt(ax * ax + ay * ay);
return [
+(ax * d / sqrt + x).toFixed(7),
+(ay * d / sqrt + y).toFixed(7)
];
};
/**
* 点(x,y)围绕中心(cx,cy)缩放times倍
*/
export let scale = function (cx, cy, times, x, y) {
return [
+(times * (x - cx) + cx).toFixed(7),
+(times * (y - cy) + cy).toFixed(7)
];
};
import { initConfig } from '../../core/config';
export let dot = function (config) {
config = initConfig({
// 前进方向
d: [1, 1],
// 中心坐标
c: [0, 0],
// 当前位置
p: [0, 0]
}, config);
let dotObj = {
// 前进方向以当前位置为中心,旋转deg度
"rotate": function (deg) {
let dPx = config.d[0] + config.p[0], dPy = config.d[1] + config.p[1];
let dP = rotate(config.p[0], config.p[1], deg, dPx, dPy);
config.d = [
dP[0] - config.p[0],
dP[1] - config.p[1]
];
return dotObj;
},
// 沿着当前前进方向前进d
"move": function (d) {
config.p = move(config.d[0], config.d[1], d, config.p[0], config.p[1]);
return dotObj;
},
// 围绕中心坐标缩放
"scale": function (times) {
config.p = scale(config.c[0], config.c[1], times, config.p[0], config.p[1]);
return dotObj;
},
// 当前位置
"value": function () {
return config.p;
}
};
return dotObj;
};