-
Notifications
You must be signed in to change notification settings - Fork 0
/
wheel.js
226 lines (205 loc) · 8.79 KB
/
wheel.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*\
title: $:/plugins/can/colourwheel/wheel.js
type: application/javascript
module-type: widget
A colour wheel to pick a hue
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
var Widget = require("$:/core/modules/widgets/widget.js").widget;
var CANWidget = function(parseTreeNode,options) {
this.initialise(parseTreeNode,options);
};
/*
Inherit from the base widget class
*/
CANWidget.prototype = new Widget();
CANWidget.prototype.getMarkerMode = function() {
this.imageSource = "nothing";
this.imageClass = "colourwheel_marker";
this.imageTooltip = this.getAttribute("tooltip");
this.imageAlt = this.getAttribute("alt");
};
CANWidget.prototype.styleSVG = function(svgid) {
var masksvg = this.document.getElementById(svgid);
masksvg.style.fill = "#999999";
masksvg.style.width = "100%";
masksvg.style.height = "100%";
};
/* All the events in here */
CANWidget.prototype.allTheEvents = function() {
var self = this;
var tempSwatchDisplay = function(classname, offset) {
var elements = self.document.getElementsByClassName(classname);
for (var ele of elements) {
ele.style.backgroundColor = getHSLA(self.angle + offset);
};
};
var getAngle = function(ev) {
self.mousex = ev.clientX - self.offsetx;
self.mousey = ev.clientY - self.offsety;
const x = self.mousex;
const y = self.mousey;
var angle = Math.round(Math.atan2(y, x) * 180 / Math.PI + 90);
if (angle < 0) {
angle += 360;
}
self.angle = angle % 360;
self.markerNode.style.transform = 'rotate(' + self.angle + 'deg)';
tempSwatchDisplay("triad1", 0);
tempSwatchDisplay("triad2", 120);
tempSwatchDisplay("triad3", 240);
/*console.log("angle: "+self.angle);*/
};
var getHSLA = function(angle) {
return "hsla("+angle+", 100%, 50%)";
};
var writeAngle = function() {
//self.wiki.setText(self.resultTiddler, self.resultField, undefined, self.angle);
self.wiki.setText(self.resultTiddler, "hue1", undefined, self.angle);
self.wiki.setText(self.resultTiddler, "hue2", undefined, self.angle + 120 );
self.wiki.setText(self.resultTiddler, "hue3", undefined, self.angle + 240 );
};
/*
Listener for grabbing bar. Get relative coords and sizes
*/
var grabBar = function(ev) {
ev.preventDefault();
self.halfWidth = Math.round(self.containerDiv.offsetWidth/2);
const rect = ev.currentTarget.getBoundingClientRect();
self.offsetx = rect.left + self.halfWidth;
self.offsety = rect.top + self.halfWidth;
getAngle(ev);
//console.log("grabbar x: "+self.mousex+", y: "+self.mousey );
self.containerDiv.addEventListener('mousemove', dragBar, false);
self.containerDiv.addEventListener('mouseup', releaseBar, false);
self.containerDiv.addEventListener('mouseleave', releaseBar, false);
};
/*
Keep changing if dragged
*/
self.containerDiv.addEventListener('mousedown', grabBar, false);
var dragBar = function(ev) {
ev.preventDefault();
ev.stopPropagation();
//console.log("dragbar1 x: "+self.mousex+", y: "+self.mousey );
//console.log("dragbar1 halfwidth: ", self.halfWidth );
getAngle(ev);
//writeAngle();
//console.log("ev.currentTarget"+ev.currentTarget );
};
var releaseBar = function(ev) {
ev.preventDefault();
self.mousex = ev.clientX - self.rectleft - self.halfWidth;
self.mousey = ev.clientY - self.recttop - self.halfWidth;
//console.log("releasebar x: "+self.mousex+", y: "+self.mousey );
getAngle(ev);
writeAngle();
/*console.log("x: "+self.mousex+", y: "+self.mousey );*/
ev.currentTarget.removeEventListener('mousemove', dragBar, false);
ev.currentTarget.removeEventListener('mouseup', releaseBar, false);
ev.currentTarget.removeEventListener('mouseleave', releaseBar, false);
};
};
/*
Render this widget into the DOM
*/
CANWidget.prototype.render = function(parent,nextSibling) {
var self = this;
this.parentDomNode = parent;
// If attributes are to be had from parameters, have to use computeAttributes()
this.computeAttributes();
this.execute();
// Create elements:
// The marker element that gets an svg from a tiddler:
this.markerNode = this.document.createElement("div");
this.markerNode.innerHTML = this.wiki.getTiddlerText(this.markerSVG);
// The outside container for the wheel widget
this.containerDiv = this.document.createElement("div");
//
this.maskDiv = this.document.createElement("div");
var innersvgtext = this.wiki.getTiddlerText(this.circlemaskSVG);
this.maskDiv.innerHTML = innersvgtext;
this.containerDiv.className = "colour-wheel";
this.maskDiv.className = "colourwheel-mask";
this.markerNode.className = "colourwheel-marker";
this.containerDiv.style.position = "relative";
this.containerDiv.style.flex = "0 0 auto";
this.containerDiv.style.width = this.widgetWidth;
this.containerDiv.style.height = this.widgetHeight;
this.containerDiv.style.background = `
conic-gradient(hsla(0, 100%, 50%, 1.0) 0deg,
hsla(60, 100%, 50%, 1.0) 60deg,
hsla(120, 100%, 50%, 1.0) 120deg,
hsla(180, 100%, 50%, 1.0) 180deg,
hsla(240, 100%, 50%, 1.0) 240deg,
hsla(300, 100%, 50%, 1.0) 300deg,
hsla(360, 100%, 50%, 1.0) 360deg)
`;
this.maskDiv.style.position = "absolute";
this.maskDiv.style.width = "100%";
this.maskDiv.style.height = "100%";
// Styles for marker
this.markerNode.style.position = "absolute";
this.markerNode.style.width = "100%";
this.markerNode.style.height = "100%";
// All the events in one function so listeners can be removed when needed
this.allTheEvents();
// Make maskDiv a child of containerDiv
this.containerDiv.appendChild(self.maskDiv);
this.containerDiv.appendChild(self.markerNode);
// this.containerDiv.appendChild(self.markerContainer);
parent.insertBefore(this.containerDiv,nextSibling);
this.renderChildren(this.maskDiv,null);
this.renderChildren(this.markerNode,null);
this.domNodes.push(this.containerDiv);
this.styleSVG("circlemasksvg");
var circlemaskpath = this.document.getElementById("circlemaskpath");
circlemaskpath.style.stroke = "#333333";
circlemaskpath.style.strokeWidth = "0.1";
circlemaskpath.style.opacity = "1";
var markersvg = this.document.getElementById("pointersvg");
markersvg.style.stroke = "#999999";
markersvg.style.width = "100%";
markersvg.style.height = "100%";
};
/*
Compute the internal state of the widget
*/
CANWidget.prototype.execute = function() {
var self = this;
// Get our widget attributes
this.resultField = this.getAttribute("field");
this.resultTiddler = this.getAttribute("tiddler");
this.mode = this.getAttribute("markerMode", "triadic");
this.getMarkerMode();
this.widgetWidth = "220px";
this.widgetHeight = "220px";
this.circlemaskSVG = "$:/plugins/can/colourwheel/simplemask.svg";
this.markerSVG = "$:/plugins/can/colourwheel/simplepointers.svg";
// Now initialise all the variables
this.mousex = 0;
this.mousey = 0;
this.halfWidth = 0;
this.angle = 0;
this.rectleft = 0;
this.recttop = 0;
// Make child widgets
this.makeChildWidgets();
};
/*
Selectively refreshes the widget if needed. Returns true if the widget or any of its children needed re-rendering
*/
CANWidget.prototype.refresh = function(changedTiddlers) {
var changedAttributes = this.computeAttributes();
if(changedAttributes.source || changedAttributes.width || changedAttributes.height || changedAttributes["class"] || changedAttributes.tooltip || changedTiddlers[this.imageSource]) {
this.refreshSelf();
return true;
} else {
return false;
}
};
exports.colourwheel = CANWidget;
})();