-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add function-templates using framebased animation
- Loading branch information
1 parent
e4f71b5
commit 1b54892
Showing
9 changed files
with
348 additions
and
0 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
5 changes: 5 additions & 0 deletions
5
packages/create-mechanic/function-templates/canvas-video-framebased/dependencies.json
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 @@ | ||
{ | ||
"dependencies": { | ||
"@mechanic-design/engine-canvas": "^2.0.0-beta.4" | ||
} | ||
} |
105 changes: 105 additions & 0 deletions
105
packages/create-mechanic/function-templates/canvas-video-framebased/index.js
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,105 @@ | ||
import { drawLoop } from "@mechanic-design/core"; | ||
|
||
export const handler = async ({ inputs, mechanic }) => { | ||
const { width, height, text, color1, color2, radiusPercentage, turns } = | ||
inputs; | ||
|
||
const center = [width / 2, height / 2]; | ||
const radius = ((height / 2) * radiusPercentage) / 100; | ||
let angle = 0; | ||
|
||
const canvas = document.createElement("canvas"); | ||
canvas.width = width; | ||
canvas.height = height; | ||
|
||
const ctx = canvas.getContext("2d"); | ||
|
||
drawLoop(({ frameCount, stop }) => { | ||
ctx.fillStyle = "#F4F4F4"; | ||
ctx.fillRect(0, 0, width, height); | ||
|
||
ctx.fillStyle = color1; | ||
ctx.beginPath(); | ||
ctx.arc( | ||
center[0], | ||
center[1], | ||
radius, | ||
Math.PI + angle, | ||
2 * Math.PI + angle, | ||
false | ||
); | ||
ctx.fill(); | ||
|
||
ctx.fillStyle = color2; | ||
ctx.beginPath(); | ||
ctx.arc(center[0], center[1], radius, 0 + angle, Math.PI + angle, false); | ||
ctx.fill(); | ||
|
||
ctx.fillStyle = "#000000"; | ||
ctx.font = `${height / 10}px sans-serif`; | ||
ctx.textAlign = "center"; | ||
ctx.strokeText(text, width / 2, height - height / 20); | ||
ctx.fillText(text, width / 2, height - height / 20); | ||
|
||
if (angle < turns * 2 * Math.PI) { | ||
mechanic.frame(canvas); | ||
angle = frameCount; | ||
} else { | ||
stop(); | ||
mechanic.done(canvas); | ||
} | ||
}, mechanic.frameRate); | ||
}; | ||
|
||
export const inputs = { | ||
width: { | ||
type: "number", | ||
default: 400, | ||
}, | ||
height: { | ||
type: "number", | ||
default: 300, | ||
}, | ||
text: { | ||
type: "text", | ||
default: "mechanic", | ||
}, | ||
color1: { | ||
type: "color", | ||
model: "hex", | ||
default: "#E94225", | ||
}, | ||
color2: { | ||
type: "color", | ||
model: "hex", | ||
default: "#002EBB", | ||
}, | ||
radiusPercentage: { | ||
type: "number", | ||
default: 40, | ||
min: 0, | ||
max: 100, | ||
slider: true, | ||
}, | ||
turns: { | ||
type: "number", | ||
default: 3, | ||
}, | ||
}; | ||
|
||
export const presets = { | ||
medium: { | ||
width: 800, | ||
height: 600, | ||
}, | ||
large: { | ||
width: 1600, | ||
height: 1200, | ||
}, | ||
}; | ||
|
||
export const settings = { | ||
engine: require("@mechanic-design/engine-canvas"), | ||
animated: true, | ||
frameRate: 60, | ||
}; |
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
5 changes: 5 additions & 0 deletions
5
packages/create-mechanic/function-templates/react-video-framebased/dependencies.json
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 @@ | ||
{ | ||
"dependencies": { | ||
"@mechanic-design/engine-react": "^2.0.0-beta.4" | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
packages/create-mechanic/function-templates/react-video-framebased/index.js
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,107 @@ | ||
import React, { useEffect, useRef } from "react"; | ||
import { useDrawLoop } from "@mechanic-design/engine-react"; | ||
|
||
export const handler = ({ inputs, mechanic }) => { | ||
const { width, height, text, color1, color2, radiusPercentage, turns } = | ||
inputs; | ||
|
||
const center = [width / 2, height / 2]; | ||
const radius = ((height / 2) * radiusPercentage) / 100; | ||
const angle = useRef(0); | ||
|
||
const isPlaying = useRef(true); | ||
const frameCount = useDrawLoop(isPlaying.current, mechanic.frameRate); | ||
|
||
useEffect(() => { | ||
if (angle.current < turns * 360) { | ||
mechanic.frame(); | ||
angle.current = frameCount; | ||
} else if (isPlaying.current) { | ||
isPlaying.current = false; | ||
mechanic.done(); | ||
} | ||
}, [frameCount]); | ||
|
||
return ( | ||
<svg width={width} height={height}> | ||
<rect fill="#F4F4F4" width={width} height={height} /> | ||
<g transform={`translate(${center[0]}, ${center[1]})`}> | ||
<g transform={`rotate(${angle.current})`}> | ||
<path | ||
d={`M ${radius} 0 | ||
A ${radius} ${radius}, 0, 0, 0, ${-radius} 0 Z`} | ||
fill={color1} | ||
/> | ||
<path | ||
d={`M ${-radius} 0 | ||
A ${radius} ${radius}, 0, 0, 0, ${radius} 0 Z`} | ||
fill={color2} | ||
/> | ||
</g> | ||
<text | ||
x={0} | ||
y={height / 2 - height / 20} | ||
textAnchor="middle" | ||
fontWeight="bold" | ||
fontFamily="sans-serif" | ||
fontSize={height / 10} | ||
> | ||
{text} | ||
</text> | ||
</g> | ||
</svg> | ||
); | ||
}; | ||
|
||
export const inputs = { | ||
width: { | ||
type: "number", | ||
default: 400, | ||
}, | ||
height: { | ||
type: "number", | ||
default: 300, | ||
}, | ||
text: { | ||
type: "text", | ||
default: "mechanic", | ||
}, | ||
color1: { | ||
type: "color", | ||
model: "hex", | ||
default: "#E94225", | ||
}, | ||
color2: { | ||
type: "color", | ||
model: "hex", | ||
default: "#002EBB", | ||
}, | ||
radiusPercentage: { | ||
type: "number", | ||
default: 40, | ||
min: 0, | ||
max: 100, | ||
slider: true, | ||
}, | ||
turns: { | ||
type: "number", | ||
default: 3, | ||
}, | ||
}; | ||
|
||
export const presets = { | ||
medium: { | ||
width: 800, | ||
height: 600, | ||
}, | ||
large: { | ||
width: 1600, | ||
height: 1200, | ||
}, | ||
}; | ||
|
||
export const settings = { | ||
engine: require("@mechanic-design/engine-react"), | ||
animated: true, | ||
frameRate: 60, | ||
}; |
5 changes: 5 additions & 0 deletions
5
packages/create-mechanic/function-templates/svg-video-framebased/dependencies.json
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 @@ | ||
{ | ||
"dependencies": { | ||
"@mechanic-design/engine-svg": "^2.0.0-beta.4" | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
packages/create-mechanic/function-templates/svg-video-framebased/index.js
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,101 @@ | ||
import { drawLoop } from "@mechanic-design/core"; | ||
|
||
export const handler = ({ inputs, mechanic }) => { | ||
const { width, height, text, color1, color2, radiusPercentage, turns } = | ||
inputs; | ||
|
||
const center = [width / 2, height / 2]; | ||
const radius = ((height / 2) * radiusPercentage) / 100; | ||
|
||
let angle = 0; | ||
drawLoop(({ frameCount, stop }) => { | ||
const svg = `<svg width="${width}" height="${height}"> | ||
<rect fill="#F4F4F4" width="${width}" height="${height}" /> | ||
<g transform="translate(${center[0]}, ${center[1]})"> | ||
<g transform="rotate(${angle})"> | ||
<path | ||
d="M ${radius} 0 | ||
A ${radius} ${radius}, 0, 0, 0, ${-radius} 0 Z" | ||
fill="${color1}" | ||
/> | ||
<path | ||
d="M ${-radius} 0 | ||
A ${radius} ${radius}, 0, 0, 0, ${radius} 0 Z" | ||
fill="${color2}" | ||
/> | ||
</g> | ||
<text | ||
x="0" | ||
y="${height / 2 - height / 20}" | ||
text-anchor="middle" | ||
font-weight="bold" | ||
font-family="sans-serif" | ||
font-size="${height / 10}" | ||
> | ||
${text} | ||
</text> | ||
</g> | ||
</svg>`; | ||
|
||
if (angle < turns * 360) { | ||
mechanic.frame(svg); | ||
angle = frameCount; | ||
} else { | ||
stop(); | ||
mechanic.done(svg); | ||
} | ||
}, mechanic.frameRate); | ||
}; | ||
|
||
export const inputs = { | ||
width: { | ||
type: "number", | ||
default: 400, | ||
}, | ||
height: { | ||
type: "number", | ||
default: 300, | ||
}, | ||
text: { | ||
type: "text", | ||
default: "mechanic", | ||
}, | ||
color1: { | ||
type: "color", | ||
model: "hex", | ||
default: "#E94225", | ||
}, | ||
color2: { | ||
type: "color", | ||
model: "hex", | ||
default: "#002EBB", | ||
}, | ||
radiusPercentage: { | ||
type: "number", | ||
default: 40, | ||
min: 0, | ||
max: 100, | ||
slider: true, | ||
}, | ||
turns: { | ||
type: "number", | ||
default: 3, | ||
}, | ||
}; | ||
|
||
export const presets = { | ||
medium: { | ||
width: 800, | ||
height: 600, | ||
}, | ||
large: { | ||
width: 1600, | ||
height: 1200, | ||
}, | ||
}; | ||
|
||
export const settings = { | ||
engine: require("@mechanic-design/engine-svg"), | ||
animated: true, | ||
frameRate: 60, | ||
}; |