Skip to content

Instantly share code, notes, and snippets.

@IUnknown68
Last active January 19, 2022 15:13
Show Gist options
  • Save IUnknown68/afb787cadaf11a22532cb54e797406f2 to your computer and use it in GitHub Desktop.
Save IUnknown68/afb787cadaf11a22532cb54e797406f2 to your computer and use it in GitHub Desktop.
Houdini example
class ArrowWorklet {
static get contextOptions() {
return { alpha: true };
}
static get inputProperties() {
return [
'--arrow-bg-color',
'--arrow-outline-color',
'--arrow-length',
'--arrow-outline-width',
'--arrow-line-join',
];
}
static get inputArguments() {
return ['*'];
}
toPx(value) {
return (new CSSUnitValue(value.value, value.unit)).to('px').value;
}
paint(ctx, size, props, args) {
const arrowLength = this.toPx(props.get('--arrow-length'));
const lw = this.toPx(props.get('--arrow-outline-width'));
const lw2 = lw / 2;
ctx.lineWidth = lw;
ctx.lineJoin = props.get('--arrow-line-join').toString().substr(1);
ctx.fillStyle = props.get('--arrow-bg-color');
ctx.strokeStyle = props.get('--arrow-outline-color');
const { width, height } = size;
const left = lw2;
const top = lw2;
const right = width - lw2;
const bottom = height - lw2;
const path = [];
switch (args[0].toString()) {
case 'top':
path.push(
[left, bottom],
[left, top + arrowLength],
[width / 2, top],
[right, top + arrowLength],
[right, bottom],
);
break;
case 'bottom':
path.push(
[right, top],
[right, bottom - arrowLength],
[width / 2, bottom],
[left, bottom - arrowLength],
[left, top],
);
break;
case 'left':
path.push(
[right, bottom],
[left + arrowLength, bottom],
[left, height / 2],
[left + arrowLength, top],
[right, top],
);
break;
default:
path.push(
[left, top],
[right - arrowLength, top],
[right, height / 2],
[right - arrowLength, bottom],
[left, bottom],
);
break;
}
this.drawPath(ctx, path);
ctx.fill();
this.drawPath(ctx, path);
ctx.stroke();
}
drawPath(ctx, path) {
ctx.beginPath();
ctx.moveTo(path[0][0], path[0][1]);
ctx.lineTo(path[1][0], path[1][1]);
ctx.lineTo(path[2][0], path[2][1]);
ctx.lineTo(path[3][0], path[3][1]);
ctx.lineTo(path[4][0], path[4][1]);
ctx.closePath();
}
}
registerPaint('arrow', ArrowWorklet);
<!doctype html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<style>
:root {
--v-arrow-padding: 0.6em;
--v-arrow-length: 1em;
--g-trans-dur-in: 0.1s;
--g-trans-dur-out: 0.6s;
}
body {
background-color: rgb(177 191 163);
font-family: sans-serif;
}
.container {
display: flex;
width: 400px;
margin: 0 auto;
flex-direction: column;
}
.arrow {
background-image: paint(arrow, right);
padding: var(--v-arrow-padding);
padding-right: calc(var(--v-arrow-length) + var(--v-arrow-padding));
transition: --arrow-outline-color calc(var(--g-trans-dur-out) * 0.6),
--arrow-length calc(var(--g-trans-dur-out) * 1.5),
--arrow-outline-width var(--g-trans-dur-out),
--arrow-bg-color var(--g-trans-dur-out),
margin calc(var(--g-trans-dur-out) * 1.5);
--arrow-bg-color: rgba(220, 220, 220, 0.5);
--arrow-outline-color: rgba(0, 127, 255, 0);
--arrow-length: 0px;
--arrow-outline-width: 1px;
margin-right: var(--v-arrow-length);
}
.arrow:hover {
--arrow-bg-color: rgba(220, 220, 220, 1);
--arrow-outline-color: rgba(0, 127, 255, 1);
--arrow-length: var(--v-arrow-length);
margin-right: 0;
--arrow-outline-width: 3px;
transition: --arrow-outline-color var(--g-trans-dur-in),
--arrow-length var(--g-trans-dur-in),
--arrow-outline-width var(--g-trans-dur-in),
--arrow-bg-color var(--g-trans-dur-in),
margin var(--g-trans-dur-in);
}
</style>
</head>
<body>
<div class="container">
<div class="arrow">lorem ipsum dolor</div>
<div class="arrow">sit amet consectetur</div>
<div class="arrow">adipisici elit sed</div>
<div class="arrow">eiusmod tempor incidunt</div>
<div class="arrow">ut labore et</div>
<div class="arrow">dolore magna aliqua</div>
<div class="arrow">ut enim ad minim</div>
<div class="arrow">veniam quis nostrud</div>
<div class="arrow">exercitation ullamco laboris</div>
<div class="arrow">nisi ut aliquid</div>
</div>
<script type="module">
CSS.registerProperty({
name: '--arrow-bg-color',
syntax: '<color>',
inherits: true,
initialValue: 'transparent',
});
CSS.registerProperty({
name: '--arrow-outline-color',
syntax: '<color>',
inherits: true,
initialValue: 'transparent',
});
CSS.registerProperty({
name: '--arrow-outline-width',
syntax: '<length>',
inherits: true,
initialValue: '1px',
});
CSS.registerProperty({
name: '--arrow-length',
syntax: '<length>',
inherits: true,
initialValue: '10px',
});
CSS.registerProperty({
name: '--arrow-line-join',
syntax: '*',
inherits: true,
initialValue: 'round',
});
CSS.paintWorklet.addModule('ArrowWorklet.js');
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment