Skip to content

Commit b289a18

Browse files
committed
Add data-forced-height/width
1 parent 5ec2502 commit b289a18

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

src/sd-node.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,32 @@ class SDNodeElement extends SDBaseElement {
7171
this.setAttribute('vertices', JSON.stringify(v));
7272
}
7373

74+
get forcedHeight() {
75+
let h = this.getAttribute('data-forced-height');
76+
if (!h) return -1;
77+
return parseInt(h);
78+
}
79+
80+
set forcedHeight(h) {
81+
let height = parseInt(h);
82+
if (!h || !Number.isInteger(height)) return;
83+
84+
this.setAttribute('data-forced-height', h);
85+
}
86+
87+
get forcedWidth() {
88+
let h = this.getAttribute('data-forced-width');
89+
if (!h) return -1;
90+
return parseInt(h);
91+
}
92+
93+
set forcedWidth(h) {
94+
let width = parseInt(h);
95+
if (!h || !Number.isInteger(width)) return;
96+
97+
this.setAttribute('data-forced-width', h);
98+
}
99+
74100
async init(parentProgram) {
75101
if (parentProgram && !this.name) {
76102
this.name = `${UNNAMED_NODE_PREFIX}${unnamedNodeIndex++}`;

src/shader-doodle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class ShaderDoodleElement extends SDNodeElement {
3434

3535
await super.init();
3636

37-
this.surface = Surface(canvas, this.program);
37+
this.surface = Surface(canvas, this.program, this);
3838
this.renderer.addSurface(this.surface);
3939
}
4040
}

src/webgl/Surface.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import getMouseOrTouch from '../utils/getMouseOrTouch.js';
55

66
import { MOUSE, MOUSEDRAG, RESOLUTION, SURFACE_UNIFORMS } from './constants.js';
77

8-
function Surface(element, program) {
8+
function Surface(element, program, sdNode) {
99
const canvas =
1010
element instanceof HTMLCanvasElement
1111
? element
@@ -74,15 +74,24 @@ function Surface(element, program) {
7474
newRect.right - newRect.width <=
7575
(window.innerWidth || document.documentElement.clientWidth);
7676

77-
if (newRect.width !== rect.width) {
78-
canvas.width = ustate[RESOLUTION].value[0] = newRect.width;
77+
const h =
78+
sdNode.forcedHeight && sdNode.forcedHeight > 0
79+
? sdNode.forcedHeight
80+
: newRect.height;
81+
const w =
82+
sdNode.forcedWidth && sdNode.forcedWidth > 0
83+
? sdNode.forcedWidth
84+
: newRect.width;
85+
86+
if (w !== rect.width) {
87+
canvas.width = ustate[RESOLUTION].value[0] = w;
7988
}
8089

81-
if (newRect.height !== rect.height) {
82-
canvas.height = ustate[RESOLUTION].value[1] = newRect.height;
90+
if (h !== rect.height) {
91+
canvas.height = ustate[RESOLUTION].value[1] = h;
8392
}
8493

85-
rect = newRect;
94+
rect = { width: canvas.width, height: canvas.height };
8695
}
8796

8897
function render(

0 commit comments

Comments
 (0)