Skip to content

Commit 36aeeb4

Browse files
committed
Make canvas square when choosing circular option
Do not renderText if running
1 parent ba1c9d3 commit 36aeeb4

1 file changed

Lines changed: 26 additions & 7 deletions

File tree

ishihara/ishihara.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,21 @@ const ctx = canvas.getContext('2d');
99

1010
const max_width = window.innerWidth * PIXEL_RATIO;
1111
const max_height = window.innerHeight * PIXEL_RATIO;
12+
const max_size = Math.min(max_width, max_height);
1213

13-
ctx.canvas.style.width = `${window.innerWidth}px`;
14-
ctx.canvas.style.height = `${window.innerHeight}px`;
15-
ctx.canvas.width = max_width;
16-
ctx.canvas.height = max_height;
14+
ctx.canvas.style.width = `${max_size / PIXEL_RATIO}px`;
15+
ctx.canvas.style.height = `${max_size / PIXEL_RATIO}px`;
16+
ctx.canvas.width = max_size;
17+
ctx.canvas.height = max_size;
1718

1819
ctx.fillStyle = 'white';
1920
ctx.fillRect(0, 0, canvas.width, canvas.height);
2021

2122
const img_canvas = document.createElement('canvas');
2223
const img_ctx = img_canvas.getContext('2d');
2324

24-
img_ctx.canvas.width = max_width;
25-
img_ctx.canvas.height = max_height;
25+
img_ctx.canvas.width = max_size;
26+
img_ctx.canvas.height = max_size;
2627
img_ctx.fillStyle = 'white';
2728
img_ctx.fillRect(0, 0, canvas.width, canvas.height);
2829

@@ -119,6 +120,7 @@ const ishihara_input = {
119120
};
120121

121122
function renderText() {
123+
if (generating) return;
122124
img_ctx.fillStyle = 'white';
123125
img_ctx.fillRect(0, 0, img_canvas.width, img_canvas.height);
124126
ctx.fillStyle = 'white';
@@ -185,7 +187,24 @@ gui.add(ishihara_input, 'load_image').name('Load image');
185187
gui.add(ishihara_input, 'color_scheme', ['General 1', 'General 2', 'General 3', 'Protanopia', 'Protanomaly', 'Viewable by all', 'Colorblind only']).name('Color scheme');
186188

187189
gui.add(ishihara_input, 'text').name('Text').onChange(() => renderText());
188-
gui.add(ishihara_input, 'circular').name('Circular');
190+
gui.add(ishihara_input, 'circular').name('Circular').onChange(value => {
191+
const w = value ? max_size : max_width;
192+
const h = value ? max_size : max_height;
193+
canvas.width = w;
194+
canvas.height = h;
195+
canvas.style.width = `${w / PIXEL_RATIO}px`;
196+
canvas.style.height = `${h / PIXEL_RATIO}px`;
197+
img_canvas.width = w;
198+
img_canvas.height = h;
199+
ctx.fillStyle = 'white';
200+
ctx.fillRect(0, 0, w, h);
201+
img_ctx.fillStyle = 'white';
202+
img_ctx.fillRect(0, 0, w, h);
203+
ishihara_input.min_radius = (w + h) / 800;
204+
ishihara_input.max_radius = (w + h) / 100;
205+
update_gui(gui);
206+
renderText();
207+
});
189208
gui.add(ishihara_input, 'resize').name('Resize');
190209
gui.add(ishihara_input, 'shape_factory', ['Circle', 'Regular polygon', 'Cross', 'Star', 'Heart']).onChange(value => {
191210
hide_gui_element(gui, 'sides', value !== 'Regular polygon' && value !== 'Star');

0 commit comments

Comments
 (0)