-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
206 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"corset": minor | ||
--- | ||
|
||
Allow using SVG templates |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import sheet, { mount } from '../../src/main.js'; | ||
|
||
const getSecondsSinceMidnight = () => (Date.now() - new Date().setHours(0, 0, 0, 0)) / 1000; | ||
const rotateFixed = (index, length) => `rotate(${(360 * index) / length})`; | ||
const rotateDyn = (rotate, fixed = 1) => `rotate(${(rotate * 360).toFixed(fixed)})`; | ||
|
||
mount(document, class { | ||
time = getSecondsSinceMidnight(); | ||
|
||
bind() { | ||
return sheet` | ||
.lines { | ||
--rotate: ${rotateFixed}; | ||
} | ||
.dyn { | ||
--rotate: ${rotateDyn}; | ||
} | ||
.hours { | ||
each-template: select(#hand-tmpl); | ||
each-items: ${new Array(12)}; | ||
--num-of-lines: 12; | ||
--length: 5; | ||
--width: 2; | ||
} | ||
.subseconds { | ||
each-template: select(#hand-tmpl); | ||
each-items: ${new Array(60)}; | ||
--num-of-lines: 60; | ||
--length: 2; | ||
--width: 1; | ||
} | ||
.lines line { | ||
--rotation: --rotate(index(), var(--num-of-lines)); | ||
class-toggle: fixed true; | ||
} | ||
.hand.dyn { | ||
attach-template: select(#hand-tmpl); | ||
} | ||
.hour { | ||
--rotation: --rotate(${((this.time / 60 / 60) % 12) / 12}); | ||
--length: 50; | ||
--width: 4; | ||
} | ||
.minute { | ||
--rotation: --rotate(${((this.time / 60) % 60) / 60}); | ||
--length: 70; | ||
--width: 3; | ||
} | ||
.second { | ||
--rotation: --rotate(${(this.time % 60) / 60}); | ||
--length: 80; | ||
--width: 2; | ||
} | ||
.subsecond { | ||
--rotation: --rotate(${this.time % 1}); | ||
--length: 85; | ||
--width: 5; | ||
} | ||
line { | ||
attr: | ||
stroke-width var(--width), | ||
transform var(--rotation); | ||
} | ||
line.fixed { | ||
attr: | ||
y1 get(var(--length), ${len => len - 95}), | ||
y2 ${-95}; | ||
} | ||
line:not(.fixed) { | ||
attr: y2 var(--length); | ||
} | ||
`; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<meta name="description" content="Clock Example"> | ||
<meta charset="utf-8"> | ||
<title>Clock</title> | ||
<link rel="stylesheet" href="./styles.css"> | ||
<script type="module" src="./app.js"></script> | ||
|
||
<div class="clock"> | ||
<svg viewBox="0 0 200 200" width="95vh"> | ||
<g transform="translate(100, 100)"> | ||
{/* static */} | ||
<circle class="text-neutral-900" r="99" fill="white" stroke="currentColor" /> | ||
<g class="hours lines"></g> | ||
<g class="subseconds lines"></g> | ||
{/* dynamic */} | ||
<g class="hour hand dyn"></g> | ||
<g class="minute hand dyn"></g> | ||
<g class="second hand dyn"></g> | ||
<g class="subsecond hand dyn"></g> | ||
</g> | ||
</svg> | ||
</div> | ||
|
||
<svg id="hand-tmpl"> | ||
<defs> | ||
<g> | ||
<line | ||
stroke="currentColor" | ||
stroke-linecap="round" /> | ||
</g> | ||
</defs> | ||
</svg> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
.clock { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
flex-wrap: wrap; | ||
height: 100vh; | ||
} | ||
|
||
.subsecond { | ||
color: silver; | ||
} | ||
|
||
.hour, | ||
.minute { | ||
color: black; | ||
} | ||
|
||
.second { | ||
color: tomato; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// @ts-check | ||
|
||
/** | ||
* @typedef {import('./types').HostElement} HostElement | ||
*/ | ||
|
||
/** | ||
* @param {HostElement} host | ||
*/ | ||
function getDocument(host) { | ||
return host.ownerDocument || host; | ||
} | ||
|
||
/** | ||
* @param {HostElement} host | ||
* @param {HTMLTemplateElement} template | ||
* @returns {DocumentFragment} | ||
*/ | ||
export function cloneTemplate(host, template) { | ||
return getDocument(host).importNode(template.content, true); | ||
} | ||
|
||
/** | ||
* @param {HostElement} host | ||
* @param {SVGElement} element | ||
* @returns {DocumentFragment} | ||
*/ | ||
export function cloneSVG(host, element) { | ||
let g = element.firstElementChild?.firstElementChild?.cloneNode(true); | ||
let frag = getDocument(host).createDocumentFragment(); | ||
while(g?.firstChild) { | ||
frag.append(g.firstChild); | ||
} | ||
return frag; | ||
} | ||
|
||
/** | ||
* @param {HTMLTemplateElement | SVGElement} template | ||
* @returns {(host: HostElement, a: any) => DocumentFragment} | ||
*/ | ||
export function createCloneElement(template) { | ||
return template.nodeName === 'TEMPLATE' ? cloneTemplate : cloneSVG; | ||
} | ||
|
||
/** | ||
* @param {HostElement} host | ||
* @param {HTMLTemplateElement | SVGElement} template | ||
*/ | ||
export function cloneElement(host, template) { | ||
return createCloneElement(template)(host, template); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters