Skip to content

Commit

Permalink
Implemented preview zoom
Browse files Browse the repository at this point in the history
  • Loading branch information
jobtalle committed May 30, 2023
1 parent 18470f9 commit 00aa914
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
9 changes: 8 additions & 1 deletion js/gl/shaderPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export class ShaderPreview extends Shader {
uniform float radius;
uniform vec2 center;
uniform float aspect;
uniform float zoom;
uniform sampler2D source;
in vec2 vUv;
Expand All @@ -22,7 +23,7 @@ export class ShaderPreview extends Shader {
if (dot(delta, delta) > radius * radius)
discard;
vec4 pixel = texture(source, uv);
vec4 pixel = texture(source, center + (uv - center) / zoom);
color = vec4(
mix(
Expand All @@ -36,6 +37,7 @@ export class ShaderPreview extends Shader {
#uniformRadius;
#uniformCenter;
#uniformAspect;
#uniformZoom;

constructor() {
super(ShaderPreview.#SHADER_FRAMGENT);
Expand All @@ -45,6 +47,7 @@ export class ShaderPreview extends Shader {
this.#uniformRadius = this.uniformLocation("radius");
this.#uniformCenter = this.uniformLocation("center");
this.#uniformAspect = this.uniformLocation("aspect");
this.#uniformZoom = this.uniformLocation("zoom")
}

setRadius(radius) {
Expand All @@ -58,4 +61,8 @@ export class ShaderPreview extends Shader {
setAspect(aspect) {
gl.uniform1f(this.#uniformAspect, aspect);
}

setZoom(zoom) {
gl.uniform1f(this.#uniformZoom, zoom);
}
}
13 changes: 13 additions & 0 deletions js/sdfMaker.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class SDFMaker {
static #INPUT_TARGET_HOVER = "hover";
static #SIZE = Number.parseInt(getComputedStyle(document.body).getPropertyValue("--size"));
static #PREVIEW_RADIUS = SDFMaker.#SIZE * .2;
static #PREVIEW_ZOOM = 2;
static #SVG_UPSCALE = 2048;

#inputTarget;
Expand All @@ -31,6 +32,7 @@ export class SDFMaker {
#previewX = -1;
#previewY = -1;
#previewVisible = false;
#previewZoom = false;

#input = gl.createTexture();
#jfa = new JFA(this.#input);
Expand Down Expand Up @@ -167,6 +169,16 @@ export class SDFMaker {
this.#updated = true;
});

inputTarget.addEventListener("mousedown", () => {
this.#previewZoom = true;
this.#updated = true;
});

window.addEventListener("mouseup", () => {
this.#previewZoom = false;
this.#updated = true;
});

this.#render();
}

Expand Down Expand Up @@ -311,6 +323,7 @@ export class SDFMaker {
if (this.#previewVisible) {
this.#shaderPreview.use();
this.#shaderPreview.setCenter(this.#previewX / SDFMaker.#SIZE, this.#previewY / SDFMaker.#SIZE);
this.#shaderPreview.setZoom(this.#previewZoom ? SDFMaker.#PREVIEW_ZOOM : 1);

gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(gl.TEXTURE_2D, this.#composite.texture);
Expand Down

0 comments on commit 00aa914

Please sign in to comment.