125 lines
4.1 KiB
JavaScript
125 lines
4.1 KiB
JavaScript
// Turns recorded frames into an animated GIF with the overlays gif_creator
|
|
// describes: orange click circles, red drag arrows, black action labels, an
|
|
// orange progress bar and a small watermark.
|
|
|
|
import { GIFEncoder, quantize, applyPalette } from "./vendor/gifenc.js";
|
|
|
|
const MAX_WIDTH = 800;
|
|
const ORANGE = "#f97316";
|
|
|
|
function drawLabel(ctx, text, w, h) {
|
|
ctx.font = "600 14px -apple-system, Segoe UI, Roboto, sans-serif";
|
|
const pad = 8;
|
|
const t = text.length > 90 ? text.slice(0, 87) + "…" : text;
|
|
const tw = Math.min(ctx.measureText(t).width, w - 2 * pad - 16);
|
|
ctx.fillStyle = "rgba(0,0,0,0.78)";
|
|
ctx.fillRect(8, h - 44, tw + 2 * pad, 28);
|
|
ctx.fillStyle = "#fff";
|
|
ctx.textBaseline = "middle";
|
|
ctx.fillText(t, 8 + pad, h - 30, w - 2 * pad - 16);
|
|
}
|
|
|
|
function drawClick(ctx, x, y) {
|
|
ctx.strokeStyle = ORANGE;
|
|
ctx.lineWidth = 3;
|
|
ctx.beginPath();
|
|
ctx.arc(x, y, 14, 0, Math.PI * 2);
|
|
ctx.stroke();
|
|
ctx.fillStyle = "rgba(249,115,22,0.35)";
|
|
ctx.beginPath();
|
|
ctx.arc(x, y, 6, 0, Math.PI * 2);
|
|
ctx.fill();
|
|
}
|
|
|
|
function drawArrow(ctx, sx, sy, ex, ey) {
|
|
ctx.strokeStyle = "#ef4444";
|
|
ctx.fillStyle = "#ef4444";
|
|
ctx.lineWidth = 3;
|
|
ctx.beginPath();
|
|
ctx.moveTo(sx, sy);
|
|
ctx.lineTo(ex, ey);
|
|
ctx.stroke();
|
|
const a = Math.atan2(ey - sy, ex - sx);
|
|
ctx.beginPath();
|
|
ctx.moveTo(ex, ey);
|
|
ctx.lineTo(ex - 14 * Math.cos(a - 0.4), ey - 14 * Math.sin(a - 0.4));
|
|
ctx.lineTo(ex - 14 * Math.cos(a + 0.4), ey - 14 * Math.sin(a + 0.4));
|
|
ctx.closePath();
|
|
ctx.fill();
|
|
}
|
|
|
|
function drawWatermark(ctx, w) {
|
|
ctx.font = "600 12px -apple-system, Segoe UI, Roboto, sans-serif";
|
|
const text = "opencode";
|
|
const tw = ctx.measureText(text).width;
|
|
ctx.fillStyle = "rgba(0,0,0,0.45)";
|
|
ctx.fillRect(w - tw - 20, 8, tw + 12, 20);
|
|
ctx.fillStyle = "#fff";
|
|
ctx.textBaseline = "middle";
|
|
ctx.fillText(text, w - tw - 14, 18);
|
|
}
|
|
|
|
async function decode(frame) {
|
|
const blob = await (await fetch(`data:image/jpeg;base64,${frame.data}`)).blob();
|
|
return createImageBitmap(blob);
|
|
}
|
|
|
|
/**
|
|
* frames: [{data (base64 jpeg), w, h (CSS px the image covers), action}]
|
|
* Returns the GIF as a Uint8Array.
|
|
*/
|
|
export async function encodeGif(frames, options = {}) {
|
|
const opt = {
|
|
showClickIndicators: true,
|
|
showDragPaths: true,
|
|
showActionLabels: true,
|
|
showProgressBar: true,
|
|
showWatermark: true,
|
|
quality: 10,
|
|
...options
|
|
};
|
|
// quality 1 (best) .. 30 (smallest): fewer palette colours as it rises.
|
|
const q = Math.min(30, Math.max(1, Number(opt.quality) || 10));
|
|
const colors = Math.max(32, Math.round(256 - (q - 1) * (224 / 29)));
|
|
|
|
const first = await decode(frames[0]);
|
|
const scale = Math.min(1, MAX_WIDTH / first.width);
|
|
const W = Math.round(first.width * scale);
|
|
const H = Math.round(first.height * scale);
|
|
first.close();
|
|
|
|
const canvas = new OffscreenCanvas(W, H);
|
|
const ctx = canvas.getContext("2d", { willReadFrequently: true });
|
|
const gif = GIFEncoder();
|
|
|
|
for (let i = 0; i < frames.length; i++) {
|
|
const f = frames[i];
|
|
const bmp = await decode(f);
|
|
ctx.fillStyle = "#fff";
|
|
ctx.fillRect(0, 0, W, H);
|
|
ctx.drawImage(bmp, 0, 0, W, H);
|
|
// Action coordinates are CSS px; the frame image covers f.w x f.h CSS px.
|
|
const k = W / (f.w || bmp.width);
|
|
bmp.close();
|
|
const a = f.action || {};
|
|
if (opt.showClickIndicators && a.kind === "click" && a.x != null) drawClick(ctx, a.x * k, a.y * k);
|
|
if (opt.showDragPaths && a.kind === "drag") drawArrow(ctx, a.sx * k, a.sy * k, a.x * k, a.y * k);
|
|
if (opt.showActionLabels && a.label) drawLabel(ctx, a.label, W, H);
|
|
if (opt.showProgressBar) {
|
|
ctx.fillStyle = "rgba(0,0,0,0.25)";
|
|
ctx.fillRect(0, H - 6, W, 6);
|
|
ctx.fillStyle = ORANGE;
|
|
ctx.fillRect(0, H - 6, (W * (i + 1)) / frames.length, 6);
|
|
}
|
|
if (opt.showWatermark) drawWatermark(ctx, W);
|
|
|
|
const { data } = ctx.getImageData(0, 0, W, H);
|
|
const palette = quantize(data, colors);
|
|
const index = applyPalette(data, palette);
|
|
const isLast = i === frames.length - 1;
|
|
gif.writeFrame(index, W, H, { palette, delay: isLast ? 2500 : a.kind === "screenshot" ? 900 : 1200, repeat: 0 });
|
|
}
|
|
gif.finish();
|
|
return gif.bytes();
|
|
}
|