Files
sausage-studio/index.html
T

1371 lines
64 KiB
HTML

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Sausage studio</title>
<style>
:root{
--bg:#F3EDE3;
--surface:#FFFCF6;
--surface-2:#EFE7DA;
--ink:#2A1D14;
--ink-soft:#73604E;
--line:#E2D7C6;
--line-strong:#CDBEA8;
--accent:#DF5A31;
--accent-strong:#C94A24;
--accent-ink:#FFF8F1;
--swatch-edge:rgba(42,29,20,.18);
--font-ui:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif;
--font-display:ui-rounded,"SF Pro Rounded","Avenir Next",-apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;
}
*,*::before,*::after{box-sizing:border-box}
html,body{margin:0}
body{
background:var(--bg);
color:var(--ink);
font-family:var(--font-ui);
font-size:14px;
line-height:1.5;
-webkit-font-smoothing:antialiased;
}
button,input{font:inherit;color:inherit}
button{cursor:pointer}
:focus-visible{outline:2px solid var(--accent);outline-offset:2px}
.app{max-width:1040px;margin:0 auto;padding:0 28px 56px}
.topbar{display:flex;align-items:center;justify-content:space-between;gap:16px;padding:20px 0 18px;border-bottom:1px solid var(--line)}
.brand{display:flex;align-items:center;gap:10px}
.brand h1{font-family:var(--font-display);font-size:20px;font-weight:600;letter-spacing:-.02em;margin:0}
.brand-mark{width:28px;height:28px;color:var(--accent);flex:none}
.layout{display:grid;grid-template-columns:minmax(0,1fr) 392px;gap:32px;align-items:start;padding-top:24px}
.preview-col{position:sticky;top:24px;min-width:0}
.preview-inner{width:100%;max-width:560px;margin:0 auto}
@media (min-width:921px){
.preview-inner{width:min(100%,calc(100vh - 238px));max-width:560px}
}
.stage{
position:relative;
aspect-ratio:1;
width:100%;
border:1px solid var(--line);
border-radius:20px;
overflow:hidden;
background-color:#F2E9DA;
transition:background-color .18s ease;
}
.stage.is-transparent{
background-color:var(--surface);
background-image:repeating-conic-gradient(var(--surface-2) 0 25%, var(--surface) 0 50%);
background-size:24px 24px;
}
.avatar{width:100%;height:100%}
.avatar svg{display:block;width:100%;height:100%}
.avatar.pop{animation:pop .2s ease-out}
@keyframes pop{from{transform:scale(.965)}to{transform:scale(1)}}
@media (prefers-reduced-motion:reduce){
.avatar.pop{animation:none}
.lid,.sausage-idle{animation:none !important}
.lid{transform:scaleY(0)}
.stage{transition:none}
}
.lid{transform-box:fill-box;transform-origin:top center;animation:blink 6.4s infinite}
@keyframes blink{
0%,92%,100%{transform:scaleY(0)}
94%,96%{transform:scaleY(1)}
}
/* idle: a gentle planted jiggle. Skew keeps the bottom edge on the floor. */
.sausage-idle{transform-box:view-box;transform-origin:50% 100%;animation:sausageIdle 5.6s ease-in-out infinite}
@keyframes sausageIdle{
0%,100%{transform:skewX(0deg) scale(1,1)}
12%{transform:skewX(1.25deg) scale(1.007,.993)}
27%{transform:skewX(.4deg) scale(1.002,.998)}
40%{transform:skewX(-1.25deg) scale(1.007,.993)}
55%{transform:skewX(-.4deg) scale(1.002,.998)}
71%{transform:skewX(.28deg) scale(1.001,.999)}
86%{transform:skewX(-.16deg) scale(1,1)}
}
.actions{display:flex;flex-wrap:wrap;gap:8px;margin-top:14px}
.btn{
display:inline-flex;align-items:center;justify-content:center;gap:8px;
height:44px;padding:0 18px;border-radius:12px;border:1px solid transparent;
font-size:14px;font-weight:560;letter-spacing:-.005em;
transition:background-color .15s ease,border-color .15s ease,transform .06s ease;
}
.btn:active{transform:translateY(1px)}
.anim-btn{flex:1 1 110px;min-width:0}
.anim-btn[aria-pressed="true"]{border-color:var(--accent);color:var(--accent-strong);background:var(--surface-2)}
.btn svg{flex:none}
.btn-primary{background:var(--accent);color:var(--accent-ink);flex:1 1 170px}
.btn-primary:hover{background:var(--accent-strong)}
.btn-default{background:var(--surface);border-color:var(--line-strong);color:var(--ink)}
.btn-default:hover{background:var(--surface-2)}
.btn-ghost{background:transparent;color:var(--ink-soft);padding:0 10px}
.btn-ghost:hover{color:var(--ink);background:var(--surface-2)}
.panel{background:var(--surface);border:1px solid var(--line);border-radius:18px;padding:2px 20px 16px;min-width:0}
.group{padding-top:16px}
.group + .group{border-top:1px solid var(--line);margin-top:16px}
.group h2{font-size:13px;font-weight:640;letter-spacing:-.005em;margin:0 0 12px;color:var(--ink)}
.field + .field{margin-top:14px}
.field-head{display:flex;align-items:baseline;justify-content:space-between;gap:12px;margin-bottom:5px}
.field-label{font-size:12.5px;color:var(--ink-soft)}
.val{font-size:12px;color:var(--ink-soft);font-variant-numeric:tabular-nums;white-space:nowrap}
.swatches{display:flex;flex-wrap:wrap;gap:8px;margin-top:8px}
.swatch{
position:relative;width:28px;height:28px;border-radius:50%;padding:0;border:0;
background:var(--sw);box-shadow:inset 0 0 0 1px var(--swatch-edge);overflow:hidden;
}
.swatch[aria-checked=true]{outline:2px solid var(--ink);outline-offset:2px}
.swatch[data-value=transparent]::after{
content:"";position:absolute;left:50%;top:2px;bottom:2px;width:1.5px;
background:var(--line-strong);transform:translateX(-50%) rotate(45deg);
}
.segmented{
display:grid;grid-auto-flow:column;grid-auto-columns:1fr;gap:2px;
background:var(--surface-2);border:1px solid var(--line);border-radius:10px;padding:3px;
}
.segmented button{
border:0;background:transparent;border-radius:7px;height:30px;padding:0 3px;
font-size:12px;font-weight:560;color:var(--ink-soft);white-space:nowrap;
}
.segmented button:hover{color:var(--ink)}
.segmented button[aria-checked=true]{background:var(--ink);color:var(--surface)}
.segmented button:focus-visible{outline-offset:-2px}
.toggle{
display:flex;align-items:center;justify-content:space-between;gap:12px;
width:100%;padding:5px 0;border:0;background:none;text-align:left;font-size:13px;
}
.toggle + .toggle{margin-top:1px}
.toggle:disabled{opacity:.38;cursor:not-allowed}
.switch{width:34px;height:20px;border-radius:10px;background:var(--line-strong);position:relative;flex:none;transition:background-color .15s ease}
.switch::after{
content:"";position:absolute;top:3px;left:3px;width:14px;height:14px;border-radius:50%;
background:var(--surface);transition:transform .15s ease;
}
.toggle[aria-checked=true] .switch{background:var(--ink)}
.toggle[aria-checked=true] .switch::after{transform:translateX(14px)}
.eyes-row{display:flex;align-items:center;gap:16px;margin-top:2px}
.eyes-sliders{flex:1;min-width:0;display:grid;gap:10px}
.eyes-sliders .field + .field{margin-top:0}
.gaze{flex:none;display:grid;justify-items:center;gap:6px}
.gaze-pad{
position:relative;width:86px;height:86px;border-radius:14px;
background:var(--surface-2);border:1px solid var(--line);
touch-action:none;cursor:grab;
}
.gaze-pad:active{cursor:grabbing}
.gaze-pad::before,.gaze-pad::after{content:"";position:absolute;background:var(--line-strong);opacity:.55}
.gaze-pad::before{left:50%;top:14px;bottom:14px;width:1px}
.gaze-pad::after{top:50%;left:14px;right:14px;height:1px}
.gaze-puck{
position:absolute;left:50%;top:50%;width:22px;height:22px;border-radius:50%;
background:var(--ink);pointer-events:none;
box-shadow:0 0 0 3px var(--surface-2);
}
.gaze-caption{font-size:11px;color:var(--ink-soft);line-height:1.2}
.range{
-webkit-appearance:none;appearance:none;width:100%;height:18px;background:transparent;cursor:pointer;
}
.range::-webkit-slider-runnable-track{height:4px;border-radius:2px;background:var(--line-strong)}
.range::-moz-range-track{height:4px;border-radius:2px;background:var(--line-strong)}
.range::-webkit-slider-thumb{
-webkit-appearance:none;width:16px;height:16px;border-radius:50%;
background:var(--ink);border:3px solid var(--surface);box-shadow:0 0 0 1px var(--line-strong);margin-top:-6px;
}
.range::-moz-range-thumb{
width:16px;height:16px;border-radius:50%;background:var(--ink);
border:3px solid var(--surface);box-shadow:0 0 0 1px var(--line-strong);
}
.range:focus-visible{outline-offset:4px;border-radius:4px}
.visually-hidden{
position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;
clip:rect(0 0 0 0);white-space:nowrap;border:0;
}
@media (max-width:920px){
.app{padding:0 20px 44px}
.layout{grid-template-columns:minmax(0,1fr);gap:24px}
.preview-col{position:static}
.preview-inner{max-width:520px}
.panel{max-width:520px;margin:0 auto;width:100%}
}
@media (max-width:420px){
.app{padding:0 14px 36px}
.topbar{padding:16px 0 14px}
.panel{padding:2px 16px 14px}
.btn{padding:0 14px}
.btn-ghost{padding:0 8px}
}
</style>
</head>
<body>
<div class="app">
<header class="topbar">
<div class="brand">
<svg class="brand-mark" viewBox="0 0 32 32" aria-hidden="true">
<rect x="9" y="3" width="14" height="26" rx="7" fill="currentColor"/>
<ellipse cx="13.4" cy="12" rx="2.5" ry="2.9" fill="#FFF8F1"/>
<ellipse cx="18.6" cy="12" rx="2.5" ry="2.9" fill="#FFF8F1"/>
<circle cx="13.8" cy="12.4" r="1.15" fill="#2A1D14"/>
<circle cx="19" cy="12.4" r="1.15" fill="#2A1D14"/>
<path d="M13.2 18.6c1.8 2.1 3.8 2.1 5.6 0" fill="none" stroke="#2A1D14" stroke-width="1.5" stroke-linecap="round"/>
</svg>
<h1>Sausage studio</h1>
</div>
</header>
<main class="layout">
<section class="preview-col" aria-label="Avatar preview">
<div class="preview-inner">
<div class="stage" id="stage">
<div class="avatar" id="avatar"></div>
</div>
<div class="actions">
<button type="button" class="btn btn-default" id="randomize">
<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
<rect x="4" y="4" width="16" height="16" rx="4"/>
<circle cx="9" cy="9" r="1.15" fill="currentColor" stroke="none"/>
<circle cx="15" cy="9" r="1.15" fill="currentColor" stroke="none"/>
<circle cx="12" cy="12" r="1.15" fill="currentColor" stroke="none"/>
<circle cx="9" cy="15" r="1.15" fill="currentColor" stroke="none"/>
<circle cx="15" cy="15" r="1.15" fill="currentColor" stroke="none"/>
</svg>
Randomize
</button>
<button type="button" class="btn btn-primary" id="download">
<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
<path d="M12 3v11"/>
<path d="m7.5 10 4.5 4.5 4.5-4.5"/>
<path d="M4 17.5v1.5a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-1.5"/>
</svg>
Download PNG
</button>
<button type="button" class="btn btn-ghost" id="reset">Reset</button>
<button type="button" class="btn btn-default anim-btn" data-anim="roll" aria-pressed="false">
<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
<circle cx="8.5" cy="12" r="4.2"/>
<circle cx="15.5" cy="12" r="4.2"/>
<circle cx="10" cy="10.4" r="1.4" fill="currentColor" stroke="none"/>
<circle cx="17" cy="10.4" r="1.4" fill="currentColor" stroke="none"/>
</svg>
<span>Rolling eyes</span>
</button>
<button type="button" class="btn btn-default anim-btn" data-anim="shock" aria-pressed="false">
<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
<path d="M12 4.5v8.5"/>
<circle cx="12" cy="18" r="1.3" fill="currentColor" stroke="none"/>
</svg>
<span>Shocked</span>
</button>
<button type="button" class="btn btn-default anim-btn" data-anim="laugh" aria-pressed="false">
<svg viewBox="0 0 24 24" width="18" height="18" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">
<circle cx="8.5" cy="8.5" r="1.4" fill="currentColor" stroke="none"/>
<circle cx="15.5" cy="8.5" r="1.4" fill="currentColor" stroke="none"/>
<path d="M5.5 13c1.2 3.4 3.6 5.2 6.5 5.2s5.3-1.8 6.5-5.2"/>
</svg>
<span>Laughing</span>
</button>
</div>
</div>
</section>
<aside class="panel" aria-label="Avatar controls">
<section class="group">
<h2>Body</h2>
<div class="field">
<span class="field-label" id="bodyColorLabel">Color</span>
<div class="swatches" id="bodyColors" role="radiogroup" aria-labelledby="bodyColorLabel"></div>
</div>
<div class="field">
<div class="field-head">
<label class="field-label" for="width">Width</label>
<span class="val" id="widthOut" aria-hidden="true">100%</span>
</div>
<input class="range" type="range" id="width" min="0.84" max="1.14" step="0.01">
</div>
<div class="field">
<div class="field-head">
<label class="field-label" for="bend">Bend</label>
<span class="val" id="bendOut" aria-hidden="true">Slight right</span>
</div>
<input class="range" type="range" id="bend" min="-1" max="1" step="0.05">
</div>
<div class="field">
<span class="field-label">Tie length <span class="val" id="tieLengthOut" aria-hidden="true"></span></span>
<input class="range" type="range" id="tieLength" min="0.55" max="1.6" step="0.05">
</div>
<div class="field">
<span class="field-label">Tie angle <span class="val" id="tieAngleOut" aria-hidden="true"></span></span>
<input class="range" type="range" id="tieAngle" min="-35" max="35" step="1">
</div>
<div class="field">
<span class="field-label" id="textureLabel">Texture</span>
<div class="segmented" id="textures" role="radiogroup" aria-labelledby="textureLabel" style="margin-top:8px"></div>
</div>
</section>
<section class="group">
<h2>Eyes</h2>
<div class="eyes-row">
<div class="gaze">
<div class="gaze-pad" id="gazePad" tabindex="0" aria-label="Eye direction. Use arrow keys or drag. Double-click to center." title="Drag to look. Double-click to center.">
<span class="gaze-puck" id="gazePuck"></span>
</div>
<span class="gaze-caption">Gaze</span>
</div>
<div class="eyes-sliders">
<div class="field">
<div class="field-head">
<label class="field-label" for="eyeGap">Spread</label>
<span class="val" id="eyeGapOut" aria-hidden="true">100%</span>
</div>
<input class="range" type="range" id="eyeGap" min="0.8" max="1.3" step="0.01">
</div>
<div class="field">
<div class="field-head">
<label class="field-label" for="pupilSize">Pupil</label>
<span class="val" id="pupilSizeOut" aria-hidden="true">100%</span>
</div>
<input class="range" type="range" id="pupilSize" min="0.55" max="1.3" step="0.01">
</div>
<div class="field">
<div class="field-head">
<label class="field-label" for="eyeSize">Size</label>
<span class="val" id="eyeSizeOut" aria-hidden="true">100%</span>
</div>
<input class="range" type="range" id="eyeSize" min="0.75" max="1.25" step="0.01">
</div>
</div>
</div>
<div class="field">
<span class="field-label" id="eyeColorLabel">Iris</span>
<div class="swatches" id="eyeColors" role="radiogroup" aria-labelledby="eyeColorLabel"></div>
</div>
<div class="field">
<span class="field-label" id="eyeShapeLabel">Shape</span>
<div class="segmented" id="eyeShapes" role="radiogroup" aria-labelledby="eyeShapeLabel" style="margin-top:8px"></div>
</div>
</section>
<section class="group">
<h2>Brows</h2>
<div class="field">
<span class="field-label" id="browStyleLabel">Shape</span>
<div class="segmented" id="browStyles" role="radiogroup" aria-labelledby="browStyleLabel" style="margin-top:8px"></div>
</div>
<div class="field">
<span class="field-label" id="browColorLabel">Hair color</span>
<div class="swatches" id="browColors" role="radiogroup" aria-labelledby="browColorLabel"></div>
</div>
</section>
<section class="group">
<h2>Mouth</h2>
<div class="field">
<span class="field-label" id="mouthStyleLabel">Shape</span>
<div class="segmented" id="mouthStyles" role="radiogroup" aria-labelledby="mouthStyleLabel" style="margin-top:8px"></div>
</div>
<div class="field">
<button type="button" class="toggle" role="switch" data-toggle="teeth" id="teethToggle" aria-checked="true">
<span>Teeth</span><span class="switch" aria-hidden="true"></span>
</button>
</div>
</section>
<section class="group">
<h2>Extras</h2>
<div class="field">
<button type="button" class="toggle" role="switch" data-toggle="glasses" aria-checked="false">
<span>Glasses</span><span class="switch" aria-hidden="true"></span>
</button>
</div>
</section>
<section class="group">
<h2>Background</h2>
<div class="field">
<span class="field-label" id="bgLabel">Color</span>
<div class="swatches" id="bgColors" role="radiogroup" aria-labelledby="bgLabel"></div>
</div>
</section>
</aside>
</main>
</div>
<script>
(function(){
"use strict";
var BODY_COLORS = [
{id:"classic", name:"Classic", light:"#F7A273", mid:"#E4653B", dark:"#B84126", deep:"#7C2A16"},
{id:"paprika", name:"Paprika", light:"#F68A62", mid:"#D9472C", dark:"#A72E1C", deep:"#6E1C10"},
{id:"bratwurst", name:"Bratwurst", light:"#F5C58A", mid:"#DCA062", dark:"#AD7440", deep:"#795029"},
{id:"smoked", name:"Smoked", light:"#C8875F", mid:"#A85B38", dark:"#7C3D21", deep:"#542814"},
{id:"weisswurst", name:"Weisswurst", light:"#FFF8EC", mid:"#EBDCC8", dark:"#C6B096", deep:"#9E8568"},
{id:"chorizo", name:"Chorizo", light:"#EF7C5D", mid:"#C63B2C", dark:"#91251B", deep:"#5E160F"}
];
var BROW_COLORS = [
{id:"espresso", name:"Espresso", hex:"#2E1B10"},
{id:"charcoal", name:"Charcoal", hex:"#2B2A28"}
];
var INK = "#33221A";
var BG_COLORS = [
{id:"cream", name:"Cream", hex:"#F2E9DA"},
{id:"butter", name:"Butter", hex:"#F6E0AE"},
{id:"sage", name:"Sage", hex:"#D6E2CD"},
{id:"sky", name:"Sky", hex:"#CBE0EC"},
{id:"blush", name:"Blush", hex:"#F3D7CF"},
{id:"clay", name:"Clay", hex:"#E5C8B3"},
{id:"ink", name:"Ink", hex:"#2C211A"},
{id:"transparent", name:"Transparent", hex:null}
];
var EYE_STYLES = [["oval","o"],["ring","O"],["caret","^^"],["heart","\u2665"]];
var EYE_COLORS = [
{id:"sky", name:"Sky", hex:"#BFD9EE"},
{id:"mint", name:"Mint", hex:"#C4E2CE"},
{id:"butter", name:"Butter", hex:"#F2E0A2"},
{id:"blush", name:"Blush", hex:"#F2CDC4"},
{id:"lilac", name:"Lilac", hex:"#D6C8EE"},
{id:"hazel", name:"Hazel", hex:"#B07A3F"},
{id:"ink", name:"Ink", hex:"#38291F"},
{id:"magma", name:"Magma", hex:"#DE5B36"}
];
var BROW_STYLES = [["none","None"],["soft","Soft"],["bold","Bold"]];
var MOUTH_STYLES = [["grin","Grin"],["laugh","Laugh"],["oh","Ooh"],["nervous","Nervous"],["sad","Sad"],["tongue","Tongue"]];
var ENDS = [["round","Round"],["tied","Tied"],["saucisson","Saucisson"]];
var DEFAULTS = {
bodyColor:"classic",
width:1,
bend:0.15,
texture:"speckles",
eyeSize:1,
gazeX:0,
gazeY:0,
eyeShape:"oval",
eyeColor:"sky",
eyeGap:1,
pupilSize:1,
browStyle:"none",
browColor:"espresso",
mouthStyle:"grin",
teeth:true,
glasses:false,
tieLength:1,
tieAngle:0,
end:"tied",
bg:"cream"
};
var state = loadState();
var avatarEl = document.getElementById("avatar");
var stageEl = document.getElementById("stage");
var gazePad = document.getElementById("gazePad");
var gazePuck = document.getElementById("gazePuck");
var teethToggle = document.getElementById("teethToggle");
var renderQueued = false;
var saveTimer = 0;
var dragging = false;
function clamp(v, a, b){ return Math.min(b, Math.max(a, v)); }
function byId(list, id){
for (var i = 0; i < list.length; i++) if (list[i].id === id) return list[i];
return list[0];
}
function loadState(){
var base = {};
for (var k in DEFAULTS) base[k] = DEFAULTS[k];
try {
var raw = localStorage.getItem("sausage-studio:v1");
if (!raw) return base;
var saved = JSON.parse(raw);
for (var key in DEFAULTS) {
if (!(key in saved)) continue;
if (typeof DEFAULTS[key] === "number") {
var n = Number(saved[key]);
if (isFinite(n)) base[key] = n;
} else if (typeof DEFAULTS[key] === "boolean") {
base[key] = !!saved[key];
} else if (typeof saved[key] === "string") {
base[key] = saved[key];
}
}
if (!BODY_COLORS.some(function(c){ return c.id === base.bodyColor; })) base.bodyColor = DEFAULTS.bodyColor;
if (!BROW_COLORS.some(function(c){ return c.id === base.browColor; })) base.browColor = DEFAULTS.browColor;
if (!BG_COLORS.some(function(c){ return c.id === base.bg; })) base.bg = DEFAULTS.bg;
if (!EYE_STYLES.some(function(o){ return o[0] === base.eyeShape; })) base.eyeShape = DEFAULTS.eyeShape;
if (base.texture !== "none" && base.texture !== "speckles") base.texture = DEFAULTS.texture;
if (!EYE_COLORS.some(function(c){ return c.id === base.eyeColor; })) base.eyeColor = DEFAULTS.eyeColor;
if (!BROW_STYLES.some(function(o){ return o[0] === base.browStyle; })) base.browStyle = DEFAULTS.browStyle;
if (!MOUTH_STYLES.some(function(o){ return o[0] === base.mouthStyle; })) base.mouthStyle = DEFAULTS.mouthStyle;
base.end = "tied"; /* the only end */
base.tieLength = clamp(base.tieLength, 0.55, 1.6);
base.tieAngle = clamp(base.tieAngle, -35, 35);
} catch (e) {}
base.width = clamp(base.width, 0.84, 1.14);
base.bend = clamp(base.bend, -1, 1);
base.eyeSize = clamp(base.eyeSize, 0.75, 1.25);
base.eyeGap = clamp(base.eyeGap, 0.8, 1.3);
base.pupilSize = clamp(base.pupilSize, 0.55, 1.3);
base.gazeX = clamp(base.gazeX, -1, 1);
base.gazeY = clamp(base.gazeY, -1, 1);
return base;
}
function saveSoon(){
clearTimeout(saveTimer);
saveTimer = setTimeout(function(){
try { localStorage.setItem("sausage-studio:v1", JSON.stringify(state)); } catch (e) {}
}, 220);
}
/* ---------- drawing ---------- */
function mulberry32(a){
return function(){
a |= 0; a = (a + 0x6D2B79F5) | 0;
var t = Math.imul(a ^ (a >>> 15), 1 | a);
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
};
}
function round(n){ return Math.round(n * 100) / 100; }
function eyeClipPath(cx, rx, ry){
return '<ellipse cx="' + cx + '" cy="0" rx="' + rx + '" ry="' + ry + '"/>';
}
function mouthPath(style){
if (style === "laugh") {
return "M -60 -7 C -31 7, 31 7, 60 -7 C 43 59, -43 59, -60 -7 Z";
}
return "M -72 -6 C -38 7, 38 7, 72 -6 C 48 40, -48 40, -72 -6 Z";
}
function mouthMarkup(s, col, browHex){
var style = s.mouthStyle;
var lip = "#4B170F";
var lipEdge = "#360F09";
var teethCol = "#FFF5E4";
var tongue = "#C4554B";
if (style === "oh") {
return '<g>' +
'<ellipse cx="0" cy="0" rx="22" ry="27" fill="' + lip + '" stroke="' + lipEdge + '" stroke-width="3"/>' +
'<ellipse cx="0" cy="14" rx="13" ry="8" fill="' + tongue + '"/>' +
'<path d="M -12 -8 Q 0 -18 12 -8" fill="none" stroke="#8A3A2A" stroke-opacity=".5" stroke-width="2"/>' +
'</g>';
}
if (style === "tongue") {
return '<g>' +
'<path d="M -52 -8 C -30 8, 30 8, 52 -8 C 40 34, -40 34, -52 -8 Z" fill="' + lip + '" stroke="' + lipEdge + '" stroke-width="3.2" stroke-linejoin="round"/>' +
'<path d="M -38 0 C -20 10, 20 10, 38 0 C 30 24, -30 24, -38 0 Z" fill="' + tongue + '"/>' +
'<path d="M -15 12 C -14 30, -8 41, 0 41 C 8 41, 14 30, 15 12 C 6 17, -6 17, -15 12 Z" fill="' + tongue + '" stroke="#A03D34" stroke-opacity=".38" stroke-width="2"/>' +
'<path d="M 0 24 Q 2.5 32 0 38" fill="none" stroke="#A03D34" stroke-opacity=".38" stroke-width="2.4" stroke-linecap="round"/>' +
'</g>';
}
if (style === "sad") {
return '<path d="M -46 4 Q 0 -26 46 4" fill="none" stroke="' + lip + '" stroke-width="7" stroke-linecap="round"/>' +
'<path d="M -46 4 q -6 2 -10 0 M 46 4 q 6 2 10 0" fill="none" stroke="' + lip + '" stroke-width="4.5" stroke-linecap="round" stroke-opacity=".55"/>';
}
if (style === "nervous") {
return '<path d="M -44 -2 Q -22 14 0 -2 T 44 -2" fill="none" stroke="' + lip + '" stroke-width="6.5" stroke-linecap="round"/>' +
'<path d="M -46 -6 q 4 6 9 7 M 46 -6 q -4 6 -9 7" fill="none" stroke="' + lip + '" stroke-width="4.5" stroke-linecap="round" stroke-opacity=".65"/>';
}
var laugh = style === "laugh";
var W = laugh ? 60 : 72;
var H = laugh ? 70 : 46;
var d = mouthPath(style);
var tb = H * (laugh ? 0.46 : 0.58);
var parts = [];
parts.push('<path d="' + d + '" fill="' + lip + '" stroke="' + lipEdge + '" stroke-width="3.2" stroke-linejoin="round"/>');
parts.push('<g clip-path="url(#mouthClip)">');
if (!s.teeth || laugh) {
parts.push('<ellipse cx="0" cy="' + (H * 0.7) + '" rx="' + (W * 0.5) + '" ry="' + (H * 0.19) + '" fill="' + tongue + '"/>');
parts.push('<ellipse cx="0" cy="' + (H * 0.64) + '" rx="' + (W * 0.3) + '" ry="' + (H * 0.07) + '" fill="#E08C82" opacity=".55"/>');
}
if (s.teeth) {
parts.push('<path d="M ' + (-W - 8) + ' -16 L ' + (W + 8) + ' -16 L ' + (W + 8) + ' ' + (tb - 3) + ' Q 0 ' + (tb + 6) + ' ' + (-W - 8) + ' ' + (tb - 3) + ' Z" fill="' + teethCol + '"/>');
var splits = [-0.58, -0.22, 0.22, 0.58];
for (var i = 0; i < splits.length; i++) {
var x = round(splits[i] * W);
parts.push('<path d="M ' + x + ' -12 L ' + x + ' ' + (tb + 4) + '" stroke="' + lip + '" stroke-opacity=".28" stroke-width="2"/>');
}
}
parts.push('</g>');
return parts.join("");
}
function browMarkup(style, color, side, gap, ry){
var cx = side * gap;
var outX = cx + side * 35;
var inX = cx - side * 33;
var base = -ry - 24;
var outY = base, inY = base, ctrlY = base - 13, weight = 12;
if (style === "none") return "";
if (style === "bold") { outY = base + 2; inY = base + 2; ctrlY = base - 17; weight = 15; }
else if (style === "soft") { outY = base + 3; inY = base + 1; ctrlY = base - 11; weight = 10; }
var d = "M " + outX + " " + outY + " Q " + (cx + side * 6) + " " + ctrlY + " " + inX + " " + inY;
return '<path d="' + d + '" fill="none" stroke="' + color + '" stroke-width="' + weight + '" stroke-linecap="round"/>';
}
function eyeMarkup(cx, idx, rx, ry, pupilR, gx, gy, col, still, irisHex, eyeShape){
var eyeStyle = eyeShape || "oval";
var irisR = pupilR * 1.18;
if (eyeStyle === "caret") {
// expression eyes: no sclera, no iris, no blink, gaze does not apply
// caret eyes only - X eyes removed
var strokes = '<path d="M ' + (cx - 21) + ' 9 Q ' + cx + ' -21 ' + (cx + 21) + ' 9" fill="none" stroke="' + INK + '" stroke-width="7.5" stroke-linecap="round"/>';
return '<g class="eye" data-cx="' + cx + '" data-gx="0" data-gy="0" data-amx="0" data-amy="0">' + strokes + '</g>';
}
var lid = still ? "" :
'<g><rect class="lid" x="' + (cx - rx - 2) + '" y="' + (-ry - 2) + '" width="' + (rx * 2 + 4) + '" height="' + (ry * 2 + 4) + '" fill="url(#bodyGrad)"/></g>';
var iris;
var hw = 0, hh = 0;
if (eyeStyle === "heart") {
// real eye with a heart-shaped iris inside; gaze + blink apply
hw = Math.max(rx * 0.62, irisR * 1.05);
hh = hw * 0.94;
iris = '<path transform="translate(' + round(cx + gx) + ' ' + round(gy) + ')" d="M 0 ' + round(hh * 0.72) +
' C ' + round(-hh * 0.2) + ' ' + round(hh * 0.52) + ', ' + round(-hw) + ' ' + round(hh * 0.16) + ', ' + round(-hw) + ' ' + round(-hh * 0.18) +
' C ' + round(-hw) + ' ' + round(-hh * 0.68) + ', ' + round(-hw * 0.36) + ' ' + round(-hh * 0.88) + ', ' + round(-hw * 0.14) + ' ' + round(-hh * 0.68) +
' C ' + round(-hw * 0.05) + ' ' + round(-hh * 0.6) + ', 0 ' + round(-hh * 0.48) + ', 0 ' + round(-hh * 0.4) +
' C 0 ' + round(-hh * 0.48) + ', ' + round(hw * 0.05) + ' ' + round(-hh * 0.6) + ', ' + round(hw * 0.14) + ' ' + round(-hh * 0.68) +
' C ' + round(hw * 0.36) + ' ' + round(-hh * 0.88) + ', ' + round(hw) + ' ' + round(-hh * 0.68) + ', ' + round(hw) + ' ' + round(-hh * 0.18) +
' C ' + round(hw) + ' ' + round(hh * 0.16) + ', ' + round(hh * 0.2) + ' ' + round(hh * 0.52) + ', 0 ' + round(hh * 0.72) + ' Z" fill="' + irisHex + '" stroke="#211612" stroke-opacity=".45" stroke-width="2"/>' +
'<circle cx="' + round(cx + gx) + '" cy="' + round(gy) + '" r="' + round(pupilR * 0.52) + '" fill="#211612"/>' +
'<circle cx="' + round(cx + gx - hw * 0.3) + '" cy="' + round(gy - hh * 0.32) + '" r="' + round(Math.max(2.6, hw * 0.11)) + '" fill="#FFFFFF" opacity=".95"/>';
} else if (eyeStyle === "ring") {
// wide-open ring: big iris with dark outline
iris = '<circle cx="' + (cx + gx) + '" cy="' + gy + '" r="' + (irisR * 1.22) + '" fill="' + irisHex + '" stroke="#211612" stroke-opacity=".55" stroke-width="2.4"/>' +
'<circle cx="' + (cx + gx) + '" cy="' + gy + '" r="' + (pupilR * 0.52) + '" fill="#211612"/>';
} else {
iris = '<circle cx="' + (cx + gx) + '" cy="' + gy + '" r="' + irisR + '" fill="' + irisHex + '" stroke="#211612" stroke-opacity=".45" stroke-width="2"/>' +
'<circle cx="' + (cx + gx) + '" cy="' + gy + '" r="' + (pupilR * 0.52) + '" fill="#211612"/>';
}
return '<g class="eye" data-cx="' + cx + '" data-gx="' + round(gx) + '" data-gy="' + round(gy) + '"' +
' data-amx="' + Math.max(0, rx - (eyeStyle === "heart" ? hw : irisR) - 2) + '" data-amy="' + Math.max(0, ry - (eyeStyle === "heart" ? hh : irisR) - 2) + '">' +
'<ellipse cx="' + cx + '" cy="0" rx="' + rx + '" ry="' + ry + '" fill="#FFF8EC"/>' +
'<g clip-path="url(#eyeClip' + idx + ')">' +
'<g class="pupil-rig">' +
iris +
(eyeStyle === "heart" ? "" :
'<circle cx="' + (cx + gx - irisR * 0.36) + '" cy="' + (gy - irisR * 0.44) + '" r="' + Math.max(2.6, irisR * 0.26) + '" fill="#FFFFFF" opacity=".95"/>' +
'<circle cx="' + (cx + gx + irisR * 0.4) + '" cy="' + (gy + irisR * 0.46) + '" r="' + Math.max(1.6, irisR * 0.13) + '" fill="#FFFFFF" opacity=".6"/>') +
'</g>' +
lid +
'</g>' +
'<ellipse cx="' + cx + '" cy="0" rx="' + rx + '" ry="' + ry + '" fill="none" stroke="' + INK + '" stroke-opacity=".38" stroke-width="2.1"/>' +
'</g>';
}
function glassesMarkup(gap, rx, ry){
// lenses are ellipses hugging the eye ellipses, shrunk if two lenses would collide
var lx = Math.min(rx + 5, gap - 7);
var ly = Math.min(ry + 5, lx * 1.42);
// temple arms: short supports leaving the outer rim, hooking down behind the face edge
var CA = 0.94, SA = 0.34;
var arms = "";
[-1, 1].forEach(function(sd){
var Ax = sd * (gap + lx * CA);
var Ay = -ly * SA;
arms += '<path d="M ' + round(Ax) + ' ' + round(Ay) +
' Q ' + round(sd * (gap + lx + 9)) + ' ' + round(Ay - 5) + ', ' + round(sd * (gap + lx + 15)) + ' ' + round(Ay - 2) +
' q ' + round(sd * 2) + ' 4 ' + round(sd * 1) + ' 10" stroke-width="4.4"/>';
});
return '<g fill="none" stroke="#2C1B11" stroke-width="5" stroke-linecap="round">' +
'<ellipse cx="' + round(-gap) + '" cy="0" rx="' + round(lx) + '" ry="' + round(ly) + '"/>' +
'<ellipse cx="' + round(gap) + '" cy="0" rx="' + round(lx) + '" ry="' + round(ly) + '"/>' +
'<path d="M ' + round(gap - lx) + ' ' + round(-ly * 0.16) + ' Q 0 ' + round(-ly * 0.52) + ' ' + round(-gap + lx) + ' ' + round(-ly * 0.16) + '"/>' +
arms +
'</g>' +
'<g fill="none" stroke="#FFFFFF" stroke-opacity=".32" stroke-width="3" stroke-linecap="round">' +
'<path d="M ' + round(-gap - lx * 0.5) + ' ' + round(-ly * 0.42) + ' l 8 -7"/>' +
'<path d="M ' + round(gap - lx * 0.5) + ' ' + round(-ly * 0.42) + ' l 8 -7"/>' +
'</g>';
}
function endShape(s, topCx, topW, topY, topSpring, col){
var style = s.end || "round";
var L = topCx - topW, R = topCx + topW, cx = topCx;
if (style === "tied") {
// the only end: a tied knot, with customizable overhang
var tieLength = (typeof s.tieLength === "number" && isFinite(s.tieLength)) ? s.tieLength : 1;
var tieAngle = (typeof s.tieAngle === "number" && isFinite(s.tieAngle)) ? s.tieAngle : 0;
var neck = 20;
var top = " C " + round(L) + " " + round(topSpring - 30) + ", " + round(cx - 72) + " " + round(topY + 16) + ", " + round(cx - neck) + " " + round(topY + 2) +
" C " + round(cx - neck + 8) + " " + round(topY - 2) + ", " + round(cx + neck - 8) + " " + round(topY - 2) + ", " + round(cx + neck) + " " + round(topY + 2) +
" C " + round(cx + 72) + " " + round(topY + 16) + ", " + round(R) + " " + round(topSpring - 30) + ", " + round(R) + " " + round(topSpring);
var fan = '<path d="M ' + round(cx - 11) + ' ' + round(topY + 5) +
' C ' + round(cx - 14) + ' ' + round(topY + 2) + ', ' + round(cx - 16) + ' ' + round(topY - 3) + ', ' + round(cx - 15) + ' ' + round(topY - 7) +
' C ' + round(cx - 14) + ' ' + round(topY - 10) + ', ' + round(cx - 12) + ' ' + round(topY - 9) + ', ' + round(cx - 10) + ' ' + round(topY - 7) +
' C ' + round(cx - 11) + ' ' + round(topY - 12) + ', ' + round(cx - 10) + ' ' + round(topY - 17) + ', ' + round(cx - 7) + ' ' + round(topY - 16) +
' C ' + round(cx - 5) + ' ' + round(topY - 15) + ', ' + round(cx - 4) + ' ' + round(topY - 12) + ', ' + round(cx - 3) + ' ' + round(topY - 9) +
' C ' + round(cx - 3) + ' ' + round(topY - 15) + ', ' + round(cx - 1) + ' ' + round(topY - 20) + ', ' + round(cx + 2) + ' ' + round(topY - 19) +
' C ' + round(cx + 4) + ' ' + round(topY - 18) + ', ' + round(cx + 4) + ' ' + round(topY - 14) + ', ' + round(cx + 4) + ' ' + round(topY - 10) +
' C ' + round(cx + 6) + ' ' + round(topY - 14) + ', ' + round(cx + 8) + ' ' + round(topY - 17) + ', ' + round(cx + 11) + ' ' + round(topY - 15) +
' C ' + round(cx + 13) + ' ' + round(topY - 13) + ', ' + round(cx + 12) + ' ' + round(topY - 9) + ', ' + round(cx + 11) + ' ' + round(topY - 6) +
' C ' + round(cx + 13) + ' ' + round(topY - 8) + ', ' + round(cx + 16) + ' ' + round(topY - 5) + ', ' + round(cx + 15) + ' ' + round(topY - 1) +
' C ' + round(cx + 15) + ' ' + round(topY + 2) + ', ' + round(cx + 13) + ' ' + round(topY + 4) + ', ' + round(cx + 11) + ' ' + round(topY + 5) +
' Z" fill="' + col.mid + '" stroke="' + col.deep + '" stroke-opacity=".6" stroke-width="2" stroke-linejoin="round"/>';
var details = '<g class="tie-fan" transform="translate(' + round(cx) + ' ' + round(topY + 1) + ') rotate(' + round(tieAngle) + ') scale(1 ' + round(tieLength * 100) / 100 + ') translate(' + round(-cx) + ' ' + round(-topY - 1) + ')">' +
'<g transform="rotate(' + round(5 + tieAngle * 0.55) + ' ' + round(cx) + ' ' + round(topY + 5) + ')">' + fan +
'<g fill="none" stroke="' + col.deep + '" stroke-opacity=".28" stroke-width="1.6" stroke-linecap="round">' +
'<path d="M ' + round(cx - 7) + ' ' + round(topY + 4) + ' C ' + round(cx - 9) + ' ' + round(topY) + ', ' + round(cx - 10) + ' ' + round(topY - 4) + ', ' + round(cx - 10) + ' ' + round(topY - 7) + '"/>' +
'<path d="M ' + round(cx - 2) + ' ' + round(topY + 4) + ' C ' + round(cx - 3) + ' ' + round(topY) + ', ' + round(cx - 3) + ' ' + round(topY - 5) + ', ' + round(cx - 3) + ' ' + round(topY - 9) + '"/>' +
'<path d="M ' + round(cx + 3) + ' ' + round(topY + 4) + ' C ' + round(cx + 4) + ' ' + round(topY) + ', ' + round(cx + 4) + ' ' + round(topY - 5) + ', ' + round(cx + 4) + ' ' + round(topY - 10) + '"/>' +
'<path d="M ' + round(cx + 7) + ' ' + round(topY + 4) + ' C ' + round(cx + 9) + ' ' + round(topY) + ', ' + round(cx + 10) + ' ' + round(topY - 3) + ', ' + round(cx + 11) + ' ' + round(topY - 6) + '"/>' +
'</g>' +
'</g>' +
'<path d="M ' + round(cx - 16) + ' ' + round(topY + 6) + ' Q ' + round(cx) + ' ' + round(topY + 11) + ' ' + round(cx + 16) + ' ' + round(topY + 6) +
'" fill="none" stroke="' + col.deep + '" stroke-opacity=".22" stroke-width="2" stroke-linecap="round"/>' +
'<g fill="none" stroke="' + col.deep + '" stroke-opacity=".2" stroke-width="1.9" stroke-linecap="round">' +
'<path d="M ' + round(cx - 4) + ' ' + round(topY + 5) + ' C ' + round(cx - 13) + ' ' + round(topY + 13) + ', ' + round(cx - 29) + ' ' + round(topY + 22) + ', ' + round(cx - 40) + ' ' + round(topY + 34) + '"/>' +
'<path d="M ' + round(cx - 2) + ' ' + round(topY + 5) + ' C ' + round(cx - 7) + ' ' + round(topY + 20) + ', ' + round(cx - 18) + ' ' + round(topY + 34) + ', ' + round(cx - 24) + ' ' + round(topY + 46) + '"/>' +
'<path d="M ' + round(cx) + ' ' + round(topY + 5) + ' C ' + round(cx - 2) + ' ' + round(topY + 22) + ', ' + round(cx - 6) + ' ' + round(topY + 40) + ', ' + round(cx - 7) + ' ' + round(topY + 54) + '"/>' +
'<path d="M ' + round(cx + 2) + ' ' + round(topY + 5) + ' C ' + round(cx + 3) + ' ' + round(topY + 22) + ', ' + round(cx + 6) + ' ' + round(topY + 40) + ', ' + round(cx + 7) + ' ' + round(topY + 54) + '"/>' +
'<path d="M ' + round(cx + 4) + ' ' + round(topY + 5) + ' C ' + round(cx + 8) + ' ' + round(topY + 20) + ', ' + round(cx + 19) + ' ' + round(topY + 34) + ', ' + round(cx + 25) + ' ' + round(topY + 46) + '"/>' +
'<path d="M ' + round(cx + 5) + ' ' + round(topY + 5) + ' C ' + round(cx + 14) + ' ' + round(topY + 13) + ', ' + round(cx + 30) + ' ' + round(topY + 22) + ', ' + round(cx + 41) + ' ' + round(topY + 34) + '"/>' +
'</g>' +
'<g fill="none" stroke="' + col.light + '" stroke-opacity=".18" stroke-width="1.6" stroke-linecap="round">' +
'<path d="M ' + round(cx - 12) + ' ' + round(topY + 9) + ' C ' + round(cx - 18) + ' ' + round(topY + 22) + ', ' + round(cx - 28) + ' ' + round(topY + 34) + ', ' + round(cx - 33) + ' ' + round(topY + 42) + '"/>' +
'<path d="M ' + round(cx + 12) + ' ' + round(topY + 9) + ' C ' + round(cx + 19) + ' ' + round(topY + 22) + ', ' + round(cx + 29) + ' ' + round(topY + 34) + ', ' + round(cx + 34) + ' ' + round(topY + 42) + '"/>' +
'</g>' +
'<path d="M ' + round(cx - 16) + ' ' + round(topY + 3) + ' Q ' + round(cx) + ' ' + round(topY + 7) + ' ' + round(cx + 16) + ' ' + round(topY + 3) +
'" fill="none" stroke="#6E4A2A" stroke-width="3.0" stroke-linecap="round"/>' +
'<path d="M ' + round(cx - 16) + ' ' + round(topY + 3) + ' Q ' + round(cx) + ' ' + round(topY + 7) + ' ' + round(cx + 16) + ' ' + round(topY + 3) +
'" fill="none" stroke="#C9A26B" stroke-width="1.0" stroke-opacity=".85" stroke-linecap="round"/>' +
'<ellipse cx="' + round(cx + 10) + '" cy="' + round(topY + 2) + '" rx="2.6" ry="2.2" fill="#C9A26B" stroke="#6E4A2A" stroke-width="1.2"/>' +
'<g fill="none" stroke="#6E4A2A" stroke-width="2.4" stroke-linecap="round">' +
'<path d="M ' + round(cx + 11) + ' ' + round(topY + 2) + ' C ' + round(cx + 14) + ' ' + round(topY - 1) + ', ' + round(cx + 16) + ' ' + round(topY) + ', ' + round(cx + 17) + ' ' + round(topY + 3) + '"/>' +
'<path d="M ' + round(cx + 11) + ' ' + round(topY + 3) + ' C ' + round(cx + 14) + ' ' + round(topY + 6) + ', ' + round(cx + 15) + ' ' + round(topY + 8) + ', ' + round(cx + 18) + ' ' + round(topY + 10) + '"/>' +
'</g>' +
'<g fill="none" stroke="#C9A26B" stroke-width="0.8" stroke-opacity=".8" stroke-linecap="round">' +
'<path d="M ' + round(cx + 11) + ' ' + round(topY + 2) + ' C ' + round(cx + 14) + ' ' + round(topY - 1) + ', ' + round(cx + 16) + ' ' + round(topY) + ', ' + round(cx + 17) + ' ' + round(topY + 3) + '"/>' +
'<path d="M ' + round(cx + 11) + ' ' + round(topY + 3) + ' C ' + round(cx + 14) + ' ' + round(topY + 6) + ', ' + round(cx + 15) + ' ' + round(topY + 8) + ', ' + round(cx + 18) + ' ' + round(topY + 10) + '"/>' +
'</g>' +
'</g>';
return {top: top, details: details};
}
return null;
}
function svgMarkup(s, still){
var col = byId(BODY_COLORS, s.bodyColor);
var browCol = byId(BROW_COLORS, s.browColor);
var eyeCol = byId(EYE_COLORS, s.eyeColor);
var eyeStyle = s.eyeStyle || s.eyeShape || "oval";
var w = Math.max(100 * s.width, 46 * s.eyeGap + 33 * s.eyeSize + 22);
var topY = 66, botY = 560;
var topCapRy = 78, botCapRy = 44;
var bend = s.bend * 26;
var topCx = 256 + bend;
var botCx = 256 - bend * 0.32;
var topW = w * 0.94;
var topSpring = topY + topCapRy;
var botSpring = botY - botCapRy;
var end = endShape(s, topCx, topW, topY, topSpring, col);
var topD = end && end.top ? end.top : (" A " + round(topW) + " " + topCapRy + " 0 0 1 " + round(topCx + topW) + " " + round(topSpring));
var bodyD = "M " + round(topCx - topW) + " " + round(topSpring) + topD +
" C " + round(topCx + topW) + " " + round(topSpring + 80) + ", " + round(botCx + w) + " " + round(botSpring - 80) + ", " + round(botCx + w) + " " + round(botSpring) +
" A " + round(w) + " " + botCapRy + " 0 0 1 " + round(botCx - w) + " " + round(botSpring) +
" C " + round(botCx - w) + " " + round(botSpring - 80) + ", " + round(topCx - topW) + " " + round(topSpring + 80) + ", " + round(topCx - topW) + " " + round(topSpring) +
" Z";
var x1 = Math.min(topCx - topW, botCx - w) - 6;
var x2 = Math.max(topCx + topW, botCx + w) + 6;
var rand = mulberry32(11);
var speckles = "";
if (s.texture === "speckles") {
for (var i = 0; i < 36; i++) {
var sx = 256 + (rand() * 2 - 1) * w * 0.95;
var sy = topY + 18 + rand() * (botY - topY - 36);
var sr = 0.8 + rand() * 2.5;
var light = rand() > 0.5;
speckles += '<circle cx="' + round(sx) + '" cy="' + round(sy) + '" r="' + round(sr) + '" fill="' + (light ? col.light : col.deep) + '" opacity="' + round(0.09 + rand() * 0.13) + '"/>';
}
}
var flatCleanup = true; /* lighting removed */
var gap = 46 * s.eyeGap;
var rx = 33 * s.eyeSize;
var ry = 39 * s.eyeSize;
var pupilR = 18 * s.pupilSize;
var gx = s.gazeX * Math.max(0, rx - pupilR - 4);
var gy = s.gazeY * Math.max(0, ry - pupilR - 4);
var eyeY = 236;
var faceX = 256 + bend * 0.55;
var faceRot = round(s.bend * 4.5);
var mouthStyle = s.mouthStyle;
var mouthY = 330;
var mw = 68;
var underFace = '';
var face = '<g class="face-rig">' +
underFace +
'<g transform="translate(' + round(faceX) + ' 0) rotate(' + faceRot + ' 0 284) translate(0 292) scale(0.95) translate(0 -284)">' +
// eye sockets + eyes
'<g transform="translate(0 ' + eyeY + ')">' +
eyeMarkup(-gap, "L", rx, ry, pupilR, gx, gy, col, still, eyeCol.hex, eyeStyle) +
eyeMarkup(gap, "R", rx, ry, pupilR, gx, gy, col, still, eyeCol.hex, eyeStyle) +
'<g class="brow-rig">' +
browMarkup(s.browStyle, browCol.hex, -1, gap, ry) +
browMarkup(s.browStyle, browCol.hex, 1, gap, ry) +
'</g>' +
(s.glasses ? '<g class="glasses-rig">' + glassesMarkup(gap, rx, ry) + '</g>' : "") +
'</g>' +
// mouth
'<g transform="translate(0 ' + mouthY + ')"><g class="mouth-rig" data-my="' + mouthY + '">' + mouthMarkup(s, col, browCol.hex) + '</g></g>' +
'</g></g>';
var endDetails = end ? end.details : "";
var defs = '<defs>' +
'<linearGradient id="bodyGrad" x1="' + round(x1) + '" y1="0" x2="' + round(x2) + '" y2="0" gradientUnits="userSpaceOnUse" class="lg-bodyGrad">' +
'<stop offset="0" stop-color="' + col.deep + '"/>' +
'<stop offset="0.09" stop-color="' + col.dark + '"/>' +
'<stop offset="0.2" stop-color="' + col.light + '"/>' +
'<stop offset="0.38" stop-color="' + col.mid + '"/>' +
'<stop offset="0.78" stop-color="' + col.mid + '"/>' +
'<stop offset="1" stop-color="' + col.deep + '"/>' +
'</linearGradient>' +
'<linearGradient id="bellyShade" x1="0" y1="' + (botY - 120) + '" x2="0" y2="' + botY + '" gradientUnits="userSpaceOnUse" class="lg-bellyShade">' +
'<stop offset="0" stop-color="' + col.deep + '" stop-opacity="0"/>' +
'<stop offset="1" stop-color="' + col.deep + '" stop-opacity=".42"/>' +
'</linearGradient>' +
'<filter id="blurMed" x="-100%" y="-100%" width="300%" height="300%" color-interpolation-filters="sRGB"><feGaussianBlur stdDeviation="6"/></filter>' +
'<filter id="blurTiny" x="-100%" y="-100%" width="300%" height="300%" color-interpolation-filters="sRGB"><feGaussianBlur stdDeviation="1.6"/></filter>' +
'<clipPath id="bodyClip"><path d="' + bodyD + '"/></clipPath>' +
'<clipPath id="eyeClipL">' + eyeClipPath(-gap, rx, ry) + '</clipPath>' +
'<clipPath id="eyeClipR">' + eyeClipPath(gap, rx, ry) + '</clipPath>' +
'<clipPath id="mouthClip"><path d="' + mouthPath(mouthStyle) + '"/></clipPath>' +
'</defs>';
var body = '<g>' +
'<path d="' + bodyD + '" fill="url(#bodyGrad)" stroke="' + INK + '" stroke-opacity=".78" stroke-width="3.4" stroke-linejoin="round"/>' +
'<g clip-path="url(#bodyClip)">' +
(speckles) +
'</g>' +
'</g>';
var markup = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" role="img" aria-label="Sausage avatar preview">' +
defs + '<g class="rig"><g class="sausage-idle">' + body + endDetails + face + '</g></g>' +
'</svg>';
if (flatCleanup) {
markup = markup.replace(/ filter="url\(#[^"]+\)"/g, "").replace(/fill="url\(#bodyGrad\)"/g, 'fill="' + col.mid + '"');
}
return markup;
}
/* ---------- animations ---------- */
var ANIM_MAKER = {
roll: function(){
var t = { d: 3800, name: "roll" };
t.tick = function(p){
var f = 1;
setGaze(Math.sin(p * Math.PI * 2 * f) * 0.96, -0.16 - Math.cos(p * Math.PI * 2 * f) * 0.24, true);
setBrows(0, -3 - Math.cos(p * Math.PI * 4) * 1.6);
setBodyRot(Math.sin((p * 5.5 - Math.sin(p * 5.5) * 0.28) * Math.PI * 2) * 1.7);
if (p >= 0.985) t.d = 0;
};
return t;
},
shock: function(){
var t = { d: 4200, name: "shock" };
var jumped = false;
t.start = function(){ t.d = 4200; jumped = false; };
t.tick = function(p){
if (p < 0.30) {
if (!jumped) {
jumped = true;
setGaze(0, -1, false);
setBrows(14, -12);
setPupilScale(0.62);
setMouth("scale", 0.8);
setFace(1.02, -2);
}
} else if (p < 0.66) {
var wx = clamp(Math.sin((p - 0.3) * Math.PI * 2 * 2.4) * 0.9, -1, 1);
setGaze(wx, -0.3, true);
setBrows(12, -10);
setPupilScale(0.78);
setMouth("scale", 0.85);
setFace(1, -1);
} else if (p < 0.90) {
var q = (p - 0.66) / 0.24;
var wx2 = clamp(Math.sin((p - 0.3) * Math.PI * 2 * 2.4) * 0.9 * (1 - q), -1, 1);
setGaze(wx2, -0.3 * (1 - q), true);
setBrows(12 * (1 - q), -10 * (1 - q));
setPupilScale(0.78 + q * 0.22);
setMouth("scale", 0.85 + q * 0.15);
setFace(1, -1 * (1 - q));
}
if (p >= 0.985) t.d = 0;
};
return t;
},
laugh: function(){
var t = { d: 4600, name: "laugh" };
t.start = function(){ t.d = 4600; };
t.tick = function(p){
if (p < 0.075) {
setBrows(-10, 2);
setFace(1, 0);
} else if (p < 0.85) {
var ph = ((p - 0.075) % 0.16) / 0.16;
var v = Math.sin(ph * Math.PI);
setBrows(-10 + v * 6, 2 - v * 7);
setFace(1 + v * 0.028, v * 2.5);
setBodyRot(Math.sin(p * Math.PI * 2 * 5.6) * 1.2);
setMouth("scale", 1 + v * 0.16);
} else {
var q2 = (p - 0.85) / 0.15;
setBrows(-10 * (1 - q2), 2 * (1 - q2));
setFace(1, 0);
setBodyRot(0);
setMouth("scale", 1);
}
if (p >= 0.985) t.d = 0;
};
return t;
}
};
var animRaf = 0;
var animTimeline = null;
var animClip = null;
var animSavedGaze = null;
var GAZE_EPS = 0.05;
function gazeNear(){
return Math.abs(state.gazeX) < GAZE_EPS && Math.abs(state.gazeY) < GAZE_EPS;
}
function setGaze(x, y, rel){
var nodes = avatarEl.querySelectorAll(".eye");
var maxAx = 1, maxAy = 1;
if (nodes.length) {
maxAx = Number(nodes[0].getAttribute("data-amx")) || 1;
maxAy = Number(nodes[0].getAttribute("data-amy")) || 1;
}
var pinch = Math.max(0, 1 - 0.14 * Math.abs(x)) * Math.max(0, 1 - 0.1 * Math.abs(y));
for (var i = 0; i < nodes.length; i++) {
var n = nodes[i];
var cx = Number(n.getAttribute("data-cx"));
var ox = rel ? x * maxAx : x * maxAx * pinch;
var oy = rel ? y * maxAy : y * maxAy * pinch;
var ax = clamp(cx + ox, cx - maxAx, cx + maxAx);
n.querySelector(".pupil-rig").setAttribute("transform",
"translate(" + round(ax - cx) + " " + round(clamp(oy, -maxAy, maxAy)) + ")");
}
if (!rel) {
if (!animSavedGaze) animSavedGaze = { x: state.gazeX, y: state.gazeY };
state.gazeX = x;
state.gazeY = y;
}
if (avatarEl.querySelector(".brow-rig")) setBrows(state.gazeX * 0.35, 0, y);
}
function setBrows(lift, dy, gazeY){
avatarEl.querySelector(".brow-rig").setAttribute("transform",
"translate(0 " + round(dy + (gazeY || 0) * -2.4) + ") rotate(" + round(lift * 0.5) + " 0 0)");
var gr = avatarEl.querySelector(".glasses-rig");
if (gr) gr.setAttribute("transform", "translate(0 " + round(dy * 0.4) + ")");
avatarEl.querySelector(".face-rig").setAttribute("transform",
"translate(0 " + round(dy * 0.5) + ")");
}
function setPupilScale(k){
var nodes = avatarEl.querySelectorAll(".pupil-rig");
for (var i = 0; i < nodes.length; i++) {
var n = nodes[i];
var eye = n.closest(".eye");
var cx = Number(eye.getAttribute("data-cx"));
var m = (n.getAttribute("transform") || "").match(/translate\([^)]*\)/);
var keep = m ? m[0] : "";
n.setAttribute("transform", keep + " translate(" + cx + " 0) scale(" + k + ") translate(" + (-cx) + " 0)");
}
}
function setMouth(mode, k){
var node = avatarEl.querySelector(".mouth-rig");
var my = Number(node.getAttribute("data-my")) || 330;
if (mode === "open") {
node.setAttribute("transform",
"translate(0 " + my + ") scale(1 " + k + ") translate(0 -" + my + ")");
} else {
node.setAttribute("transform",
"translate(0 " + my + ") scale(" + k + ") translate(0 -" + my + ")");
}
}
function setFace(s, dy){
avatarEl.querySelector(".face-rig").setAttribute("transform",
"translate(0 " + round(dy) + ") scale(" + round(s, 3) + " 1)");
}
function setBodyRot(deg){
avatarEl.querySelector(".rig").setAttribute("transform",
"rotate(" + round(deg, 2) + " 256 552)");
}
function stopAnimation(){
if (animRaf) { cancelAnimationFrame(animRaf); animRaf = 0; }
animTimeline = null;
animClip = null;
syncAnimButtons();
if (animSavedGaze) {
state.gazeX = animSavedGaze.x;
state.gazeY = animSavedGaze.y;
animSavedGaze = null;
if (!gazeNear()) commit();
}
}
function playAnimation(key){
stopAnimation();
if (!avatarEl.querySelector(".rig") || !avatarEl.querySelector(".face-rig") ||
!avatarEl.querySelector(".brow-rig") || !avatarEl.querySelector(".mouth-rig") ||
avatarEl.querySelectorAll(".pupil-rig").length < 2) return;
avatarEl.innerHTML = svgMarkup(state, false);
syncFace(state);
var maker = ANIM_MAKER[key];
if (!maker) return;
animClip = maker();
syncAnimButtons();
if (animClip.start) animClip.start();
animTimeline = performance.now();
var prev = 0;
var step = function(now){
var el = now - animTimeline;
var p = clamp(el / animClip.d, 0, 1);
animClip.tick(p);
if (p >= 1 || animClip.d === 0) {
stopAnimation();
avatarEl.innerHTML = svgMarkup(state, false);
syncFace(state);
return;
}
animRaf = requestAnimationFrame(step);
};
animRaf = requestAnimationFrame(step);
}
/* ---------- ui ---------- */
function fillSwatches(container, options, key, current){
var html = "";
for (var i = 0; i < options.length; i++) {
var o = options[i];
html += '<button type="button" class="swatch" role="radio" data-choice="' + key + '" data-value="' + o.id + '"' +
' aria-checked="' + (current === o.id) + '" aria-label="' + o.name + '" title="' + o.name + '"' +
' style="--sw:' + (o.hex || o.mid || "transparent") + '"></button>';
}
container.innerHTML = html;
}
function fillSegments(container, options, key, current){
var html = "";
for (var i = 0; i < options.length; i++) {
html += '<button type="button" role="radio" data-choice="' + key + '" data-value="' + options[i][0] + '"' +
' aria-checked="' + (current === options[i][0]) + '">' + options[i][1] + '</button>';
}
container.innerHTML = html;
}
function buildUI(){
fillSwatches(document.getElementById("bodyColors"), BODY_COLORS, "bodyColor", state.bodyColor);
fillSwatches(document.getElementById("browColors"), BROW_COLORS, "browColor", state.browColor);
fillSwatches(document.getElementById("eyeColors"), EYE_COLORS, "eyeColor", state.eyeColor);
fillSegments(document.getElementById("eyeShapes"), EYE_STYLES, "eyeShape", state.eyeShape);
fillSwatches(document.getElementById("bgColors"), BG_COLORS, "bg", state.bg);
fillSegments(document.getElementById("browStyles"), BROW_STYLES, "browStyle", state.browStyle);
fillSegments(document.getElementById("mouthStyles"), MOUTH_STYLES, "mouthStyle", state.mouthStyle);
fillSegments(document.getElementById("textures"), [["none","None"],["speckles","Speckles"]], "texture", state.texture);
}
function bendText(){
if (Math.abs(state.bend) < 0.03) return "Straight";
return (state.bend < 0 ? "Left " : "Right ") + Math.round(Math.abs(state.bend) * 100) + "%";
}
function gazeText(){
var x = state.gazeX, y = state.gazeY;
if (Math.abs(x) < 0.2 && Math.abs(y) < 0.2) return "center";
var h = Math.abs(x) < 0.2 ? "" : (x < 0 ? "left" : "right");
var v = Math.abs(y) < 0.2 ? "" : (y < 0 ? "up" : "down");
return [h, v].filter(Boolean).join(" ");
}
function syncFace(s){
if (!avatarEl.querySelector(".brow-rig")) return;
setBrows(s.gazeX * 0.35, 0, s.gazeY);
}
function syncControls(){
var choices = document.querySelectorAll("[data-choice]");
for (var i = 0; i < choices.length; i++) {
var el = choices[i];
el.setAttribute("aria-checked", String(state[el.getAttribute("data-choice")] === el.getAttribute("data-value")));
}
var toggles = document.querySelectorAll("[data-toggle]");
for (var j = 0; j < toggles.length; j++) {
var t = toggles[j];
t.setAttribute("aria-checked", String(!!state[t.getAttribute("data-toggle")]));
}
var teethActive = state.mouthStyle === "grin" || state.mouthStyle === "laugh";
teethToggle.disabled = !teethActive;
teethToggle.setAttribute("aria-disabled", String(!teethActive));
teethToggle.title = teethActive ? "" : "This mouth does not take teeth";
var widthEl = document.getElementById("width");
widthEl.value = state.width;
widthEl.setAttribute("aria-valuetext", Math.round(state.width * 100) + "% wide");
document.getElementById("widthOut").textContent = Math.round(state.width * 100) + "%";
var bendEl = document.getElementById("bend");
bendEl.value = state.bend;
bendEl.setAttribute("aria-valuetext", bendText());
document.getElementById("bendOut").textContent = bendText();
setRange("eyeSize", "eyeSizeOut");
gazePuck.style.transform = "translate(calc(-50% + " + round(state.gazeX * 31) + "px), calc(-50% + " + round(state.gazeY * 31) + "px))";
gazePad.setAttribute("aria-label", "Eye direction, " + gazeText() + ". Use arrow keys or drag. Double-click to center.");
var bg = byId(BG_COLORS, state.bg);
stageEl.classList.toggle("is-transparent", !bg.hex);
stageEl.style.backgroundColor = bg.hex || "";
}
function setRange(key, outId){
var el = document.getElementById(key);
var pct = Math.round(state[key] * 100);
el.value = state[key];
el.setAttribute("aria-valuetext", pct + "%");
document.getElementById(outId).textContent = pct + "%";
}
function render(){
avatarEl.innerHTML = svgMarkup(state, false);
syncFace(state);
}
function scheduleRender(){
if (renderQueued) return;
renderQueued = true;
requestAnimationFrame(function(){
renderQueued = false;
render();
});
}
function commit(){
saveSoon();
syncControls();
scheduleRender();
}
function randomize(){
var r = Math.random;
function pick(list){ return list[Math.floor(r() * list.length)]; }
state.bodyColor = pick(BODY_COLORS).id;
state.width = 0.92 + r() * 0.2;
state.bend = r() * 1.4 - 0.7;
state.eyeSize = 0.9 + r() * 0.28;
state.eyeColor = pick(EYE_COLORS).id;
state.eyeGap = 0.9 + r() * 0.24;
state.pupilSize = 0.8 + r() * 0.4;
state.gazeX = r() * 1.2 - 0.6;
state.gazeY = r() * 0.8 - 0.4;
state.eyeShape = pick(EYE_STYLES)[0];
state.cap = r() > 0.7;
state.chain = r() > 0.82;
state.browStyle = pick(BROW_STYLES)[0];
state.browColor = pick(BROW_COLORS).id;
state.mouthStyle = pick(MOUTH_STYLES)[0];
state.teeth = r() > 0.22;
state.glasses = r() > 0.78;
state.tieLength = 0.8 + r() * 0.55;
state.tieAngle = Math.round((r() * 50 - 25));
state.end = "tied"; /* the only end */
state.texture = pick([["speckles","speckles"],["none","none"],["speckles","speckles"]])[0];
state.gloss = r() > 0.08;
commit();
avatarEl.classList.remove("pop");
void avatarEl.offsetWidth;
avatarEl.classList.add("pop");
}
function reset(){
state = {};
for (var k in DEFAULTS) state[k] = DEFAULTS[k];
try { localStorage.removeItem("sausage-studio:v1"); } catch (e) {}
commit();
}
function download(){
var svg = svgMarkup(state, true);
var blob = new Blob([svg], {type:"image/svg+xml;charset=utf-8"});
var url = URL.createObjectURL(blob);
var img = new Image();
img.onload = function(){
var size = 1024;
var canvas = document.createElement("canvas");
canvas.width = size;
canvas.height = size;
var ctx = canvas.getContext("2d");
var bg = byId(BG_COLORS, state.bg);
if (bg.hex) {
ctx.fillStyle = bg.hex;
ctx.fillRect(0, 0, size, size);
}
ctx.drawImage(img, 0, 0, size, size);
URL.revokeObjectURL(url);
canvas.toBlob(function(b){
if (!b) return;
var a = document.createElement("a");
var objUrl = URL.createObjectURL(b);
a.href = objUrl;
a.download = "sausage-avatar.png";
document.body.appendChild(a);
a.click();
a.remove();
setTimeout(function(){ URL.revokeObjectURL(objUrl); }, 1500);
}, "image/png");
};
img.onerror = function(){ URL.revokeObjectURL(url); };
img.src = url;
}
function setGazeFromPointer(e){
var rect = gazePad.getBoundingClientRect();
var x = ((e.clientX - rect.left) / rect.width) * 2 - 1;
var y = ((e.clientY - rect.top) / rect.height) * 2 - 1;
var len = Math.sqrt(x * x + y * y);
if (len > 1) { x /= len; y /= len; }
state.gazeX = round(clamp(x, -1, 1));
state.gazeY = round(clamp(y, -1, 1));
commit();
}
/* ---------- events ---------- */
document.addEventListener("click", function(e){
var choice = e.target.closest("[data-choice]");
if (choice) {
state[choice.getAttribute("data-choice")] = choice.getAttribute("data-value");
commit();
return;
}
var toggle = e.target.closest("[data-toggle]");
if (toggle && !toggle.disabled) {
var key = toggle.getAttribute("data-toggle");
state[key] = !state[key];
commit();
}
});
var rangeKeys = ["width", "bend", "eyeSize", "eyeGap", "pupilSize", "tieLength", "tieAngle"];
rangeKeys.forEach(function(key){
var el = document.getElementById(key);
el.addEventListener("input", function(){
state[key] = parseFloat(el.value);
saveSoon();
syncControls();
scheduleRender();
});
});
document.querySelectorAll("[role=radiogroup]").forEach(function(group){
group.addEventListener("keydown", function(e){
if (["ArrowLeft", "ArrowRight", "ArrowUp", "ArrowDown"].indexOf(e.key) === -1) return;
var items = Array.prototype.slice.call(group.querySelectorAll("[role=radio]"));
var index = items.indexOf(document.activeElement);
if (index < 0) return;
e.preventDefault();
var dir = (e.key === "ArrowRight" || e.key === "ArrowDown") ? 1 : -1;
var next = items[(index + dir + items.length) % items.length];
next.focus();
next.click();
});
});
gazePad.addEventListener("pointerdown", function(e){
dragging = true;
try { gazePad.setPointerCapture(e.pointerId); } catch (err) {}
setGazeFromPointer(e);
});
gazePad.addEventListener("pointermove", function(e){
if (dragging) setGazeFromPointer(e);
});
gazePad.addEventListener("pointerup", function(){ dragging = false; });
gazePad.addEventListener("pointercancel", function(){ dragging = false; });
gazePad.addEventListener("dblclick", function(){
state.gazeX = 0;
state.gazeY = 0;
commit();
});
gazePad.addEventListener("keydown", function(e){
var step = e.shiftKey ? 0.4 : 0.15;
var handled = true;
if (e.key === "ArrowLeft") state.gazeX = clamp(state.gazeX - step, -1, 1);
else if (e.key === "ArrowRight") state.gazeX = clamp(state.gazeX + step, -1, 1);
else if (e.key === "ArrowUp") state.gazeY = clamp(state.gazeY - step, -1, 1);
else if (e.key === "ArrowDown") state.gazeY = clamp(state.gazeY + step, -1, 1);
else if (e.key === "Home") { state.gazeX = 0; state.gazeY = 0; }
else handled = false;
if (!handled) return;
e.preventDefault();
state.gazeX = round(state.gazeX);
state.gazeY = round(state.gazeY);
commit();
});
document.getElementById("randomize").addEventListener("click", randomize);
document.getElementById("download").addEventListener("click", function(){
stopAnimation();
download();
});
document.getElementById("reset").addEventListener("click", function(){
stopAnimation();
reset();
});
var animBtns = document.querySelectorAll("[data-anim]");
function syncAnimButtons(){
for (var i = 0; i < animBtns.length; i++) {
var b = animBtns[i];
b.setAttribute("aria-pressed",
String(!!(animClip && animClip.name === b.getAttribute("data-anim"))));
}
}
Array.prototype.forEach.call(animBtns, function(btn){
btn.addEventListener("click", function(){
var key = btn.getAttribute("data-anim");
if (animClip && animClip.name === key) { stopAnimation(); return; }
playAnimation(key);
});
});
document.addEventListener("visibilitychange", function(){
if (document.hidden) stopAnimation();
});
buildUI();
syncControls();
render();
window.SausageStudio = { get state(){ return state; }, set state(next){ state = next; commit(); } };
})();
</script>
</body>
</html>