Skip to content

The open-source full-stack studio for Opus 5.5 motion video. Reverse engineering for any video. Code video with HTML: recreate, edit and render. BYOA + BYOK · Motion.md · Images, Video, Music & Voice.

10.2 KB
Raw Blame
'use strict';
const canvas=document.querySelector('canvas'),ctx=canvas.getContext('2d');
const W=1000,H=600,TAU=Math.PI*2;
let P={title:CONFIG.demoTitle,accent:CONFIG.accent||CONFIG.palette[2]||'#888',strength:1},time=0;
const clamp=(v,a=0,b=1)=>Math.max(a,Math.min(b,v)),mix=(a,b,u)=>a+(b-a)*u,ease=x=>1-Math.pow(1-clamp(x),3),phase=(t,a,b)=>ease((t-a)/(b-a)),sin=Math.sin,cos=Math.cos;
const frac=x=>x-Math.floor(x),rand=i=>frac(Math.sin(i*127.1+311.7)*43758.5453);
const ink=()=>CONFIG.palette[0]==='#ffffff'||CONFIG.palette[0]==='#fafafa'?'#19191e':CONFIG.palette[1];
function rect(x,y,w,h,c,r=0){ctx.fillStyle=c;ctx.beginPath();ctx.roundRect(x,y,Math.max(0,w),Math.max(0,h),Math.max(0,Math.min(r,w/2,h/2)));ctx.fill();}
function line(x1,y1,x2,y2,c='#999',width=1){ctx.strokeStyle=c;ctx.lineWidth=width;ctx.beginPath();ctx.moveTo(x1,y1);ctx.lineTo(x2,y2);ctx.stroke();}
function poly(points,fill,stroke){ctx.beginPath();points.forEach(([x,y],i)=>i?ctx.lineTo(x,y):ctx.moveTo(x,y));ctx.closePath();if(fill){ctx.fillStyle=fill;ctx.fill()}if(stroke){ctx.strokeStyle=stroke;ctx.lineWidth=1;ctx.stroke()}}
function ellipse(x,y,rx,ry,c,stroke){ctx.beginPath();ctx.ellipse(x,y,Math.max(.01,rx),Math.max(.01,ry),0,0,TAU);if(c){ctx.fillStyle=c;ctx.fill()}if(stroke){ctx.strokeStyle=stroke;ctx.lineWidth=1;ctx.stroke()}}
function text(str,x,y,size=32,color='#fff',align='center',font='Arial',weight=400,maxWidth){ctx.fillStyle=color;ctx.font=`${weight} ${size}px ${font}`;ctx.textAlign=align;ctx.textBaseline='middle';if(maxWidth)ctx.fillText(String(str),x,y,maxWidth);else ctx.fillText(String(str),x,y);}
function label(str,x,y,c='#888'){text(str,x,y,12,c,'left','monospace')}
function fittedText(str,x,y,size,c,max=850,font='Arial',weight=400){ctx.font=`${weight} ${size}px ${font}`;text(str,x,y,Math.min(size,size*max/Math.max(1,ctx.measureText(str).width)),c,'center',font,weight)}
function wrap(str,max=32){const words=String(str).split(' ');let out=[''];for(const w of words){let n=out.length-1;if((out[n]+w).length>max&&out[n])out.push(w);else out[n]+=(out[n]?' ':'')+w;}return out;}
function multiline(str,x,y,size,c,width=32){const rows=wrap(str,width);rows.forEach((r,i)=>fittedText(r,x,y+(i-(rows.length-1)/2)*size*1.15,size,c,850));}
function saved(fn){ctx.save();fn();ctx.restore()}
function at(x,y,s=1,a=0,fn){saved(()=>{ctx.translate(x,y);ctx.rotate(a);ctx.scale(s,s);fn()})}
function alpha(a,fn){saved(()=>{ctx.globalAlpha*=clamp(a);fn()})}
function bg(c=CONFIG.palette[0]){rect(0,0,W,H,c)}
function grid(c='#9992',step=50){for(let x=0;x<W;x+=step)line(x,0,x,H,c);for(let y=0;y<H;y+=step)line(0,y,W,y,c)}
function glow(x,y,r,color){const g=ctx.createRadialGradient(x,y,0,x,y,r);g.addColorStop(0,color);g.addColorStop(1,color.slice(0,7)+'00');rect(x-r,y-r,r*2,r*2,g)}
function gradient(c1,c2,x=0,y=0,w=W,h=H){const g=ctx.createLinearGradient(x,y,x+w,y+h);g.addColorStop(0,c1);g.addColorStop(1,c2);rect(x,y,w,h,g)}
function star(x,y,r,c,n=6,rot=0){poly(Array.from({length:n*2},(_,i)=>[x+cos(i*Math.PI/n+rot)*r*(i%2?.36:1),y+sin(i*Math.PI/n+rot)*r*(i%2?.36:1)]),c)}
function flower(x,y,r,c,rot=0){at(x,y,1,rot,()=>{for(let j=0;j<6;j++)at(0,0,1,j*TAU/6,()=>ellipse(0,-r*.52,r*.27,r*.55,c));ellipse(0,0,r*.18,r*.18,'#efcb77')})}
function figma(x,y,s,collapse=1,t=0){const colors=['#f24e1e','#a259ff','#ff7262','#1abcfe','#0acf83'];at(x,y,s,0,()=>{[[0,0],[1,0],[0,1],[1,1],[0,2]].forEach(([a,b],i)=>{let dx=(a-.5)*35+(1-collapse)*(rand(i+3)-.5)*600,dy=(b-1)*35+(1-collapse)*(rand(i+9)-.5)*400;at(dx,dy,1,(1-collapse)*sin(t+i),()=>rect(-17,-17,34,34,colors[i],i===1?12:18))})})}
function simpleMark(x,y,s,c=P.accent){at(x,y,s,0,()=>{rect(-24,-24,22,48,c,8);rect(2,-24,22,48,c,8);ellipse(-10,0,3,8,'#fff');ellipse(10,0,3,8,'#fff')})}
function cursor(x,y,c='#fff'){poly([[x,y],[x+4,y+28],[x+12,y+18],[x+22,y+18]],c,'#333')}
function ui(x,y,w,h,c=P.accent,title=P.title,progress=1){saved(()=>{ctx.shadowColor='#0002';ctx.shadowBlur=18;rect(x,y,w,h,'#fafafa',8)});rect(x,y,w,26,'#e9e9e7',8);[0,1,2].forEach(i=>ellipse(x+12+i*12,y+13,3,3,['#e9766b','#edc160','#78b393'][i]));label(title,x+50,y+14,'#666');rect(x+10,y+36,w*.19,h-48,'#ececea',3);for(let i=0;i<5;i++)rect(x+18,y+48+i*21,w*.13,5,'#c7c7c5');const ww=w*.67;rect(x+w*.25,y+42,ww,10,'#333');for(let i=0;i<6;i++)if(progress>i/7){let xx=x+w*.25+(i%3)*(ww/3),yy=y+68+Math.floor(i/3)*(h*.28);rect(xx,yy,ww/3-8,h*.24,i===0?c:'#e5e6e3',4);rect(xx+5,yy+h*.24-14,ww/4,4,'#999')}}
function face(x,y,r,c,t=0,shape=0){at(x,y,1,sin(t)*.15,()=>{if(shape===1){for(let i=0;i<4;i++)ellipse(cos(i*TAU/4+Math.PI/4)*r*.3,sin(i*TAU/4+Math.PI/4)*r*.3,r*.7,r*.7,c)}else ellipse(0,0,r,r,c);rect(-r*.35,-r*.24,r*.16,r*.35,'#fff',r*.08);rect(r*.12,-r*.24,r*.16,r*.35,'#fff',r*.08)})}
function person(x,y,s,c='#222'){at(x,y,s,0,()=>{ellipse(0,-42,16,20,c);poly([[-18,-20],[18,-20],[26,35],[-24,35]],c);line(-10,30,-18,78,c,12);line(10,30,21,78,c,12);line(-18,-10,-40,30,c,10);line(18,-10,40,25,c,10)})}
function portrait(x,y,w,h,i,t=0){saved(()=>{rect(x,y,w,h,['#dfddd8','#c8d6db','#ebd5ca'][i%3]);ctx.beginPath();ctx.rect(x,y,w,h);ctx.clip();person(x+w*.5,y+h*.53,h/155,['#202226','#4f454b','#273e3f'][i%3]);rect(x+8,y+h-18,w*.4,3,'#fff8')})}
const maskCache=new Map();function wordPoints(value,size=84){let key=value+'|'+size;if(maskCache.has(key))return maskCache.get(key);const c=document.createElement('canvas');c.width=800;c.height=220;const q=c.getContext('2d');q.fillStyle='#fff';q.font=`700 ${size}px Arial`;q.textAlign='center';q.textBaseline='middle';q.fillText(value,400,110,750);let a=q.getImageData(0,0,800,220).data,pts=[];for(let y=0;y<220;y+=5)for(let x=0;x<800;x+=5)if(a[(y*800+x)*4+3]>100)pts.push([x-400,y-110]);maskCache.set(key,pts);return pts;}
function progressText(value,u){return value.slice(0,Math.floor(clamp(u)*value.length))}
function useTitle(fallback){return P.title===CONFIG.demoTitle?fallback:P.title}
function stageAt(t){let n=0;CONFIG.stages.forEach((s,i)=>{if(t>=s.start)n=i});return n}
function stageProgress(t){let n=stageAt(t),a=CONFIG.stages[n].start,b=CONFIG.stages[n+1]?.start||CONFIG.duration;return clamp((t-a)/(b-a))}
function paletteDemo(){bg('#f3f2ef');label('COLOR & TYPOGRAPHY',40,42,'#777');const colors=CONFIG.palette;colors.forEach((c,i)=>{rect(40+i*920/colors.length,90,920/colors.length-5,170,c,6);label(c,45+i*920/colors.length,280,'#555')});fittedText(CONFIG.brand||CONFIG.labels[0]||'Motion',500,380,72,'#242424',900);text(CONFIG.font,500,465,18,'#555','center','Arial',400,900);label('保持配色层级与字形特征',40,555,'#777')}
function objectsDemo(){bg('#f3f2ef');label('OBJECTS & RELATIONSHIPS',40,38,'#777');const n=CONFIG.objects.length;CONFIG.objects.forEach((v,i)=>{const x=70+(i%3)*315,y=90+Math.floor(i/3)*145;line(x+30,y+35,x+285,y+35,'#aaa');ellipse(x+20,y+35,19,19,P.accent);text(String(i+1).padStart(2,'0'),x+20,y+35,15,'#fff');text(v,x+55,y+35,20,'#242424','left','Arial',400,240);label(i===0?'主体 / 语义锚点':'独立对象 / 共享编排',x+55,y+73,'#888')});label('按角色组织元素,保留主次与编排关系。',40,555,'#777')}
function storyboardDemo(){bg('#efeeeb');const stages=CONFIG.stages.length>9?Array.from({length:9},(_,i)=>CONFIG.stages[Math.round(i*(CONFIG.stages.length-1)/8)]):CONFIG.stages;const cols=3,rows=Math.ceil(stages.length/cols),ch=(H-65)/rows;stages.forEach((s,i)=>{let x=20+(i%cols)*326,y=20+Math.floor(i/cols)*ch;const next=CONFIG.stages[CONFIG.stages.indexOf(s)+1]?.start||CONFIG.duration;at(x,y,Math.min(.306,(ch-35)/H),0,()=>{ctx.beginPath();ctx.rect(0,0,W,H);ctx.clip();drawScene(mix(s.start,next,.48))});label(`${s.start.toFixed(2)}s  ${s.label}`,x,y+ch-12,'#555')})}
function trackValue(tr,t){if(!tr||!tr.valid.some(([a,b])=>t>=a&&t<=b))return null;const ss=tr.samples;let i=ss.findIndex(x=>x[0]>=t);if(i<=0)return i<0?ss.at(-1)[1]:ss[0][1];const [a,v]=ss[i-1],[b,w]=ss[i],u=clamp((t-a)/(b-a));return Array.isArray(v)?v.map((x,j)=>mix(x,w[j],u)):mix(v,w,u)}
function measurementDemo(t){bg('#151c1d');const sw=CONFIG.sourceCanvas.width,sh=CONFIG.sourceCanvas.height,k=Math.min(940/sw,480/sh),ox=(W-sw*k)/2,oy=70;label('实际观测与拟合轨道 / 无原帧背景',25,30,'#c0d8cb');saved(()=>{ctx.translate(ox,oy);ctx.scale(k,k);ctx.strokeStyle='#6c8f8144';ctx.lineWidth=1/k;ctx.strokeRect(0,0,sw,sh);const rows=DATA.ocr||[],row=rows.reduce((best,r)=>Math.abs(r.t-t)<Math.abs((best?.t??1e9)-t)?r:best,null);if(row&&Math.abs(row.t-t)<.52){for(const r of row.text){let[x,y,w,h]=r.box;ctx.strokeStyle='#8adbb688';ctx.strokeRect(x*sw,y*sh,w*sw,h*sh);text(r.text,x*sw,y*sh-7/k,12/k,'#b2e7cf','left','Arial',400,Math.max(40/k,w*sw))}}for(const target of [...new Set(DATA.tracks.filter(x=>!x.target.startsWith('text-')).map(x=>x.target))]){const ts=DATA.tracks.filter(x=>x.target===target),get=p=>trackValue(ts.find(x=>x.property===p),t),c=get('center'),w=get('width'),h=get('height');if(!c||w===null||h===null)continue;const r=get('cornerRadius')||0;rect(c[0]-w/2,c[1]-h/2,w,h,'#5bc8d755',r);ctx.strokeStyle='#76ddeb';ctx.lineWidth=2/k;ctx.beginPath();ctx.roundRect(c[0]-w/2,c[1]-h/2,w,h,Math.min(r,w/2,h/2));ctx.stroke();const ct=ts.find(x=>x.property==='center');for(const [a,b]of ct.valid){ctx.beginPath();let started=false;for(const [time,v]of ct.samples){if(time<a||time>b||time>t)continue;started?ctx.lineTo(...v):ctx.moveTo(...v);started=true}ctx.strokeStyle='#e7bf67';ctx.lineWidth=1.5/k;ctx.stroke()}ellipse(c[0],c[1],3/k,3/k,'#edcb83');label(target+' · 有效观测区间',c[0]-w/2,c[1]-h/2-15/k,'#90e5ec')}});label('位置:源画面像素 · 按源画幅等比显示 · 只在 valid 区间绘制',25,575,'#a6bdb2')}

const mode=document.body.dataset.mode;const clipStart=Number(document.body.dataset.start||0);const duration=mode==='clip'?Number(document.body.dataset.end)-clipStart:mode==='tokens'||mode==='objects'||mode==='storyboard'?.1:mode==='action'?CONFIG.actionRange[1]-CONFIG.actionRange[0]:CONFIG.duration;
function renderAt(t){time=clamp(Number(t)||0,0,duration);ctx.setTransform(canvas.width/W,0,0,canvas.height/H,0,0);ctx.globalAlpha=1;ctx.filter='none';ctx.shadowBlur=0;ctx.clearRect(0,0,W,H);if(mode==='tokens')paletteDemo();else if(mode==='objects')objectsDemo();else if(mode==='storyboard')storyboardDemo();else if(mode==='measurement')measurementDemo(time);else drawScene(mode==='clip'?time+clipStart:mode==='action'?time+CONFIG.actionRange[0]:time)}
window.motionDemo={duration,renderAt,setParams(values){P={...P,...values};renderAt(time)}};