Skip to content

Commit

Permalink
Rainbow border
Browse files Browse the repository at this point in the history
Note: requires tweaking crank.js to accept xmlns:

bikeshaving/crank#268
  • Loading branch information
canadaduane committed Nov 4, 2023
1 parent 116ec89 commit 12a4779
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 76 deletions.
30 changes: 30 additions & 0 deletions src/color.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import Color from "colorjs.io";
import { getScroll } from "./drag.js";

const C = 0.8;

export function getColorFromScreenCoord(x, y, w, h) {
const a = (x / w - 0.5) * C;
const b = (y / h - 0.5) * C;
return new Color("oklab", [1, a, b]).toString({
format: "rgba",
});
}

export function getColorFromPolarCoord(phi, lightness = 1.0) {
const a = C * Math.cos(phi);
const b = C * Math.sin(phi);
return new Color("oklab", [lightness, a, b]).toString({
format: "rgba",
});
}

export function getColorFromWorldCoord(x, y) {
const { left, top } = getScroll();
return getColorFromScreenCoord(
x - left,
y - top,
window.innerWidth,
window.innerHeight
);
}
74 changes: 0 additions & 74 deletions src/colorwheel.js

This file was deleted.

4 changes: 3 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
spiralAddend,
} from "./constants.js";
import { nanoid } from "nanoid";
import { getColorFromWorldCoord } from "./colorwheel.js";
import { getColorFromWorldCoord } from "./color.js";
import { RainbowBorder } from "./rainbow-border.js";
import {
applyNodeToShapes,
removeShape,
Expand Down Expand Up @@ -580,6 +581,7 @@ function* Svg({ nodes: initNodes = [], shapes: initShapes = [] }) {
}
})}
</svg>
<${RainbowBorder} w=${winW} h=${winH} borderWidth=${2} />
${htmlShapes.map(([shapeId, shape]) => {
switch (shape.type) {
case "circle":
Expand Down
2 changes: 1 addition & 1 deletion src/orb.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
orbRectHeight,
stringLengthTransition,
} from "./constants.js";
import { getColorFromWorldCoord } from "./colorwheel.js";
import { getColorFromWorldCoord } from "./color.js";

function isFirefox() {
return navigator.userAgent.toLowerCase().indexOf("firefox") > -1;
Expand Down
54 changes: 54 additions & 0 deletions src/rainbow-border.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { html } from "./utils.js";
import { getColorFromPolarCoord } from "./color.js";

export function* RainbowBorder() {
const N = 12;
const radians = Array.from({ length: N }, (_, i) => (i * Math.PI * 2) / N);

const css = html`<style>
div.gradient {
width: 100%;
height: 100%;
background-image: conic-gradient(
from 90deg,
${radians
.map((r, i) => `${getColorFromPolarCoord(r)} ${(i / 12) * 100}%`)
.join(",\n")}
);
}
</style>`;

for (const { w, h, borderWidth: b } of this) {
yield html`
${css}
<svg
viewBox="0 0 ${w} ${h}"
style=${{
"width": `${w}px`,
"height": `${h}px`,
"pointer-events": "none",
"position": "fixed",
"left": 0,
"top": 0,
}}
xmlns="http://www.w3.org/2000/svg"
>
<mask id="mask">
<rect x="0" y="0" width=${w} height=${h} fill="white" />
<rect
x=${b}
y=${b}
width=${w - b * 2}
height=${h - b * 2}
rx=${b}
ry=${b}
/>
</mask>
<foreignObject width="100%" height="100%" mask="url(#mask)">
<div class="gradient" xmlns="http://www.w3.org/1999/xhtml"></div>
</foreignObject>
</svg>
`;
}
}

0 comments on commit 12a4779

Please sign in to comment.