-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathmain.js
109 lines (97 loc) · 3.02 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const {Path, Color} = require("scenegraph");
async function starShapeFunction(selection) {
const dialog = createDialog();
try {
const value = await dialog.showModal();
if (3 <= value.vertex && 0 < value.radius1 && 0 < value.radius2) {
const vertex = value.vertex;
const radius1 = value.radius1;
const radius2 = value.radius2;
const points = [];
const angle = 360 / vertex;
// 頂点の座標を求める
for (let i = 0; i < vertex; i++) {
const rad1 = (angle * i - angle / 2) * Math.PI / 180;
const x1 = radius1 * Math.cos(rad1);
const y1 = radius1 * Math.sin(rad1);
points.push({x:x1, y:y1});
const rad2 = angle * i * Math.PI / 180;
const x2 = radius2 * Math.cos(rad2);
const y2 = radius2 * Math.sin(rad2);
points.push({x:x2, y:y2});
}
// path data のテキスト作成
let pathData = `M${points[0].x},${points[0].y}`;
for (let i = 1; i < points.length; i++) {
pathData += `L${points[i].x},${points[i].y}`
}
pathData += 'Z';
// path を追加
const wedge = new Path();
wedge.pathData = pathData;
wedge.fill = new Color("#000000");
wedge.translation = {x: radius1, y: radius1};
selection.insertionParent.addChild(wedge);
} else {
console.log("Illegal value");
}
} catch (err) {
console.log("ESC dismissed dialog");
}
}
function createDialog() {
document.body.innerHTML = `
<style>
#dialog form {
width: 300px;
}
</style>
<dialog id="dialog">
<form method="dialog">
<label>
<span>Number of vertices</span>
<input uxp-quiet="true" type="text" id="vertex" value="3"/>
</label>
<label>
<span>radius 1</span>
<input uxp-quiet="true" type="text" id="radius1" value="100"/>
</label>
<label>
<span>radius 2</span>
<input uxp-quiet="true" type="text" id="radius2" value="50"/>
</label>
<footer>
<button id="cancel">Cancel</button>
<button type="submit" id="ok" uxp-variant="cta">OK</button>
</footer>
</form>
</dialog>
`;
const dialog = document.querySelector('#dialog');
const form = document.querySelector('#dialog form');
const cancel = document.querySelector('#cancel');
const ok = document.querySelector('#ok');
const vertex = document.querySelector('#vertex');
const radius1 = document.querySelector('#radius1');
const radius2 = document.querySelector('#radius2');
const submit = (e) => {
dialog.close({vertex: parseInt(vertex.value), radius1: parseInt(radius1.value), radius2: parseInt(radius2.value)});
}
cancel.addEventListener("click", () => {
dialog.close();
});
ok.addEventListener("click", e => {
submit();
e.preventDefault();
});
form.addEventListener("submit", e => {
submit();
e.preventDefault();
})
return dialog;
}
module.exports = {
commands: {
starShapeCommand: starShapeFunction
}
};