Canvas-Video-Html5-2D / ChatBubColorCyc.html
mondk's picture
Upload 6 files
5adca9e verified
Raw History Blame Contribute Delete
10.8 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title></title>
<style>
html,body{margin:0;padding:0;width:100%;height:100%;background:#FFFFFF;overflow:hidden;}
canvas{display:block;}
</style>
</head>
<body>
<canvas id="c"></canvas>
<script>
(function(){
"use strict";
var cv = document.getElementById('c');
var ctx = cv.getContext('2d');
var DW = 1600, DH = 900;
var dpr = 1, sc = 1, ox = 0, oy = 0;
var FONT = '"Inter","Helvetica Neue","Segoe UI",Roboto,Helvetica,Arial,sans-serif';
function resize(){
dpr = Math.min(window.devicePixelRatio || 1, 2);
cv.width = Math.max(1, Math.floor(window.innerWidth * dpr));
cv.height = Math.max(1, Math.floor(window.innerHeight * dpr));
cv.style.width = window.innerWidth + 'px';
cv.style.height = window.innerHeight + 'px';
sc = Math.min(cv.width / DW, cv.height / DH);
ox = (cv.width - DW * sc) * 0.5;
oy = (cv.height - DH * sc) * 0.5;
}
window.addEventListener('resize', resize);
resize();
/* ---------- helpers ---------- */
function clamp(v,a,b){ return v<a?a:(v>b?b:v); }
function ss(e0,e1,x){ var t=clamp((x-e0)/(e1-e0),0,1); return t*t*(3-2*t); }
function easeInOut(t){ return t<0.5 ? 4*t*t*t : 1-Math.pow(-2*t+2,3)/2; }
function easeOutBack(t){
var c1=1.70158, c3=c1+1;
var u=t-1;
return 1 + c3*u*u*u + c1*u*u;
}
function fontStr(w,s){ return w+' '+s+'px '+FONT; }
function hx(h){
return [parseInt(h.slice(1,3),16),parseInt(h.slice(3,5),16),parseInt(h.slice(5,7),16)];
}
function mix(a,b,u){
var r=Math.round(a[0]+(b[0]-a[0])*u),
g=Math.round(a[1]+(b[1]-a[1])*u),
bb=Math.round(a[2]+(b[2]-a[2])*u);
return 'rgb('+r+','+g+','+bb+')';
}
function rrect(c,x,y,w,h,r){
r = Math.min(r, w*0.5, h*0.5);
c.beginPath();
c.moveTo(x+r,y);
c.lineTo(x+w-r,y); c.quadraticCurveTo(x+w,y,x+w,y+r);
c.lineTo(x+w,y+h-r); c.quadraticCurveTo(x+w,y+h,x+w-r,y+h);
c.lineTo(x+r,y+h); c.quadraticCurveTo(x,y+h,x,y+h-r);
c.lineTo(x,y+r); c.quadraticCurveTo(x,y,x+r,y);
c.closePath();
}
function wrap(text,maxW){
var words = text.split(' '), lines = [], cur = '';
for(var i=0;i<words.length;i++){
var test = cur ? (cur+' '+words[i]) : words[i];
if(ctx.measureText(test).width > maxW && cur){ lines.push(cur); cur = words[i]; }
else cur = test;
}
if(cur) lines.push(cur);
return lines;
}
function layoutWords(text,maxW){
var words = text.split(' ');
var spaceW = ctx.measureText(' ').width;
var lines = [], cur = [], x = 0;
for(var i=0;i<words.length;i++){
var w = ctx.measureText(words[i]).width;
if(cur.length && x + spaceW + w > maxW){ lines.push(cur); cur = []; x = 0; }
cur.push({ i:i, word:words[i], x:x });
x += w + spaceW;
}
if(cur.length) lines.push(cur);
return lines;
}
function fitSize(text,weight,start,maxW){
var s = start;
ctx.font = fontStr(weight,s);
while(ctx.measureText(text).width > maxW && s > 12){ s -= 2; ctx.font = fontStr(weight,s); }
return s;
}
/* ---------- timeline constants ---------- */
var LOOP = 8.15;
var S1_TYPE = 0.85;
var S1_CUT = 1.60;
var S2_POP = 1.70, POP_DUR = 0.42;
var SLIDE_A = 3.85, SLIDE_B = 4.40;
var STREAM_A = 3.95, STREAM_B = 4.80, WORD_RAMP = 0.18;
var ICON_A = 5.00, ICON_DUR = 0.30;
var S3_CUT = 5.45;
var S4_TYPE = 1.05;
var FADE_A = 7.95, FADE_B = 8.12;
/* ---------- content ---------- */
var TITLE1 = "Make it your own";
var TITLE2 = "Expert intelligence for everyone";
var QUESTION = "If I had custom chat bubbles, what color do you think they would be?";
var ANSWER = "A hunter green would suit you best\u2014polished, distinctive, and balancing your analytical side with creativity. If you want a softer, quirkier option, go for muted sage green.";
var AWORDS = ANSWER.split(' ');
var RESP_X = 150, RESP_W = 920, RESP_TOP = 455, RESP_LH = 46;
/* ---------- bubble colour cycle ---------- */
var KEYS = [
{ t:2.15, f:hx('#F2F2F1'), x:hx('#111111'), g:0 },
{ t:2.45, f:hx('#E9E4F8'), x:hx('#6B4FA8'), g:1 },
{ t:2.75, f:hx('#DCEDFB'), x:hx('#1F3D7A'), g:2 },
{ t:3.05, f:hx('#FBDFD0'), x:hx('#A8502A'), g:3 },
{ t:3.35, f:hx('#FADCE9'), x:hx('#B03A78'), g:4 },
{ t:3.65, f:hx('#FDF3D3'), x:hx('#7A7A2E'), g:5 },
{ t:3.95, f:hx('#DBF5DF'), x:hx('#1F6B3A'), g:6 }
];
function bubbleCol(t){
if(t <= KEYS[0].t){
return { fill:mix(KEYS[0].f,KEYS[0].f,0), txt:mix(KEYS[0].x,KEYS[0].x,0), g:KEYS[0].g };
}
for(var i=0;i<KEYS.length-1;i++){
if(t < KEYS[i+1].t){
var u = ss(KEYS[i].t, KEYS[i+1].t, t);
return {
fill: mix(KEYS[i].f, KEYS[i+1].f, u),
txt: mix(KEYS[i].x, KEYS[i+1].x, u),
g: KEYS[i].g + (KEYS[i+1].g - KEYS[i].g) * u
};
}
}
var L = KEYS[KEYS.length-1];
return { fill:mix(L.f,L.f,0), txt:mix(L.x,L.x,0), g:L.g };
}
/* ---------- utility icons ---------- */
function iconCopy(){
rrect(ctx,-8,-2,13,15,3); ctx.stroke();
ctx.save(); ctx.fillStyle='#FFFFFF'; rrect(ctx,-3,-8,13,15,3); ctx.fill(); ctx.restore();
rrect(ctx,-3,-8,13,15,3); ctx.stroke();
}
function iconSpeaker(){
ctx.beginPath();
ctx.moveTo(-10,-4); ctx.lineTo(-5,-4); ctx.lineTo(1,-9);
ctx.lineTo(1,9); ctx.lineTo(-5,4); ctx.lineTo(-10,4);
ctx.closePath(); ctx.stroke();
ctx.beginPath(); ctx.arc(3,0,5,-0.95,0.95); ctx.stroke();
ctx.beginPath(); ctx.arc(3,0,9.5,-0.95,0.95); ctx.stroke();
}
function thumbPath(){
ctx.beginPath();
ctx.moveTo(-1,12);
ctx.lineTo(-1,-1);
ctx.lineTo(2,-4);
ctx.quadraticCurveTo(6,-9,5,-11.5);
ctx.quadraticCurveTo(2,-13.5,1,-9.5);
ctx.lineTo(0,-4);
ctx.lineTo(8,-4);
ctx.quadraticCurveTo(12,-4,11,0);
ctx.lineTo(9,12);
ctx.closePath();
ctx.stroke();
}
function iconUp(){ thumbPath(); }
function iconDown(){ ctx.save(); ctx.scale(1,-1); thumbPath(); ctx.restore(); }
function iconRegen(){
var r=8.5, a0=0.7, a1=5.5;
ctx.beginPath(); ctx.arc(0,0,r,a0,a1); ctx.stroke();
var ex=r*Math.cos(a1), ey=r*Math.sin(a1);
var ang=Math.atan2(-Math.cos(a1), Math.sin(a1)) + Math.PI;
for(var k=0;k<2;k++){
var s = (k===0)?0.7:-0.7;
ctx.beginPath();
ctx.moveTo(ex+Math.cos(ang+s)*6.5, ey+Math.sin(ang+s)*6.5);
ctx.lineTo(ex,ey);
ctx.stroke();
}
}
function iconShare(){
ctx.beginPath();
ctx.moveTo(-8,2); ctx.lineTo(-8,10); ctx.lineTo(8,10); ctx.lineTo(8,2);
ctx.stroke();
ctx.beginPath(); ctx.moveTo(0,8); ctx.lineTo(0,-9); ctx.stroke();
ctx.beginPath(); ctx.moveTo(-5,-4); ctx.lineTo(0,-9); ctx.lineTo(5,-4); ctx.stroke();
}
var ICONS = [iconCopy,iconSpeaker,iconUp,iconDown,iconRegen,iconShare];
/* ---------- scenes ---------- */
function drawTitleTyping(text, typeDur, alpha){
if(alpha <= 0) return;
var n = Math.min(text.length, Math.floor(clamp(text.length * (0 + 0), 0, 0)));
n = Math.min(text.length, Math.floor(clamp(typeDur>0 ? (typeDur) : 0,0,0)));
return n;
}
function scene1(t){
if(t >= S1_CUT) return;
var n = Math.min(TITLE1.length, Math.floor(TITLE1.length * clamp(t / S1_TYPE,0,1)));
if(n <= 0) return;
ctx.save();
ctx.globalAlpha = 1;
ctx.textAlign = 'center';
ctx.fillStyle = '#0B0B0B';
ctx.font = fontStr(700, 80);
ctx.fillText(TITLE1.slice(0,n), DW/2, DH/2);
ctx.restore();
}
function scene4(t){
if(t < S3_CUT) return;
var alpha = 1 - ss(FADE_A, FADE_B, t);
if(alpha <= 0) return;
var n = Math.min(TITLE2.length, Math.floor(TITLE2.length * clamp((t - S3_CUT) / S4_TYPE, 0, 1)));
if(n <= 0) return;
var shown = TITLE2.slice(0,n);
ctx.save();
ctx.globalAlpha = alpha;
ctx.textAlign = 'center';
ctx.fillStyle = '#0B0B0B';
var size = fitSize(TITLE2, 700, 74, 1300);
ctx.font = fontStr(700, size);
ctx.fillText(shown, DW/2, DH/2);
ctx.restore();
}
function chatScene(t){
if(t < S2_POP || t >= S3_CUT) return;
/* --- bubble --- */
var col = bubbleCol(t);
ctx.save();
ctx.textAlign = 'left';
ctx.font = fontStr(400, 27);
var lines = wrap(QUESTION, 430);
var maxL = 0, i, j;
for(i=0;i<lines.length;i++) maxL = Math.max(maxL, ctx.measureText(lines[i]).width);
var padX = 36, padY = 32, lh = 36;
var bw = maxL + padX*2;
var bh = lines.length*lh + padY*2;
var right = 1470;
var slide = easeInOut(clamp((t - SLIDE_A) / (SLIDE_B - SLIDE_A), 0, 1));
var top = 300 - 140 * slide;
var cx = right - bw/2, cy = top + bh/2;
var pop = clamp((t - S2_POP)/POP_DUR, 0, 1);
var popS = easeOutBack(pop);
if(popS > 0){
var s = popS * (1 + 0.010 * col.g);
ctx.translate(cx, cy); ctx.scale(s, s); ctx.translate(-cx, -cy);
ctx.fillStyle = col.fill;
rrect(ctx, right - bw, top, bw, bh, 30);
ctx.fill();
ctx.fillStyle = col.txt;
ctx.font = fontStr(400, 27);
ctx.textAlign = 'left';
for(i=0;i<lines.length;i++){
ctx.fillText(lines[i], right - bw + padX, top + padY + lh/2 + i*lh);
}
}
ctx.restore();
/* --- streamed response --- */
ctx.save();
ctx.textAlign = 'left';
ctx.fillStyle = '#3B3B3B';
ctx.font = fontStr(400, 30);
var blocks = layoutWords(ANSWER, RESP_W);
for(i=0;i<blocks.length;i++){
var line = blocks[i];
for(j=0;j<line.length;j++){
var w = line[j];
var appear = STREAM_A + (w.i / Math.max(1,(AWORDS.length-1))) * (STREAM_B - STREAM_A);
var a = clamp((t - appear) / WORD_RAMP, 0, 1);
if(a <= 0) continue;
ctx.globalAlpha = a;
ctx.fillText(w.word, RESP_X + w.x, RESP_TOP + i*RESP_LH);
}
}
ctx.globalAlpha = 1;
/* --- icon row --- */
var ia = clamp((t - ICON_A) / ICON_DUR, 0, 1);
if(ia > 0){
var ease = 1 - Math.pow(1 - ia, 3);
var iy = RESP_TOP + blocks.length*RESP_LH + 40 + (1 - ease)*8;
ctx.save();
ctx.lineWidth = 2;
ctx.strokeStyle = '#8E8E8E';
ctx.lineJoin = 'round';
ctx.lineCap = 'round';
for(i=0;i<ICONS.length;i++){
ctx.save();
ctx.globalAlpha = ease;
ctx.translate(RESP_X + 14 + i*56, iy);
ICONS[i]();
ctx.restore();
}
ctx.restore();
}
ctx.restore();
}
/* ---------- main loop ---------- */
var t0 = performance.now();
function draw(t){
ctx.setTransform(1,0,0,1,0,0);
ctx.fillStyle = '#FFFFFF';
ctx.fillRect(0,0,cv.width,cv.height);
ctx.setTransform(sc,0,0,sc,ox,oy);
ctx.textBaseline = 'middle';
ctx.textAlign = 'left';
ctx.lineJoin = 'round';
ctx.lineCap = 'round';
scene1(t);
chatScene(t);
scene4(t);
}
function frame(now){
var t = ((now - t0) / 1000) % LOOP;
if(t < 0) t += LOOP;
draw(t);
requestAnimationFrame(frame);
}
requestAnimationFrame(frame);
})();
</script>
</body>
</html>