184 lines
9.6 KiB
JavaScript
184 lines
9.6 KiB
JavaScript
|
|
/* Robot Clash - paint booth: draw your own artwork onto the robot, the 3D robot updates live.
|
||
|
|
The drawing is a transparent 512x256 layer: left half = top view, right half = side view
|
||
|
|
(front of the robot to the right in both). render3d.js puts it over the paint colour.
|
||
|
|
|
||
|
|
const painter = RC.Painter({ t, layer, getPaint(), onLive(), onDone(), onCancel(), sfx, view });
|
||
|
|
painter.open() / painter.close()
|
||
|
|
RC.layerFromData(canvas, dataURL, done) restore a saved drawing
|
||
|
|
Needs kit-core.js. Panel is built into #shop (replaces the parts column while open). */
|
||
|
|
(function (root) {
|
||
|
|
'use strict';
|
||
|
|
const RC = root.RC, $ = Kit.$;
|
||
|
|
const LW = 512, LH = 256;
|
||
|
|
const COLORS = ['#ffffff', '#c8ccd4', '#6b7280', '#15171b', '#d8452b', '#ff7a1a', '#f2c230', '#5fae3b',
|
||
|
|
'#2e9c8f', '#2a6fd8', '#6a3fb0', '#d94f9a', '#8a5a3a', '#ffd9a0', '#00e5ff', '#b6ff3a'];
|
||
|
|
const STAMPS = ['★', '⚡', '⚙', '✚', '●', '▲', '♛', '☢', '🔥', '💀', '👁', '🎯'];
|
||
|
|
|
||
|
|
RC.layerFromData = (cv, data, done) => {
|
||
|
|
const c = cv.getContext('2d');
|
||
|
|
c.clearRect(0, 0, LW, LH);
|
||
|
|
if (!data) { if (done) done(); return; }
|
||
|
|
const im = new Image();
|
||
|
|
im.onload = () => { c.drawImage(im, 0, 0, LW, LH); if (done) done(); };
|
||
|
|
im.onerror = () => { if (done) done(); };
|
||
|
|
im.src = data;
|
||
|
|
};
|
||
|
|
|
||
|
|
RC.Painter = function (opt) {
|
||
|
|
const t = opt.t, layer = opt.layer, lc = layer.getContext('2d');
|
||
|
|
let tool = 'brush', color = '#ffffff', size = 14, stamp = '★', text = '', mirror = true;
|
||
|
|
let undo = [], drawing = null, panel = null, before = null, pending = false;
|
||
|
|
|
||
|
|
function build() {
|
||
|
|
panel = document.createElement('div');
|
||
|
|
panel.id = 'painter';
|
||
|
|
panel.innerHTML = `
|
||
|
|
<div class="pt-head"><h3>🎨 ${t('pt_title')}</h3><div class="seg" id="ptView">
|
||
|
|
${['top', 'right', 'left', 'front', 'free'].map(v => `<button data-v="${v}" class="${v === 'free' ? 'on' : ''}">${t('pt_view_' + v)}</button>`).join('')}</div></div>
|
||
|
|
<div class="pt-canvas"><span class="pt-lab l">${t('pt_top')} ▸</span><span class="pt-lab r">${t('pt_side')} ▸</span><canvas id="ptCv" width="${LW}" height="${LH}"></canvas></div>
|
||
|
|
<div class="pt-tools" id="ptTools">
|
||
|
|
${[['brush', '🖌️'], ['spray', '💨'], ['erase', '🧽'], ['stamp', '⭐'], ['text', '🔤'], ['fill', '🪣']].map(([k, g]) => `<button data-tool="${k}" title="${t('pt_' + k)}">${g} ${t('pt_' + k)}</button>`).join('')}
|
||
|
|
<label class="pt-chk"><input type="checkbox" id="ptMirror" ${mirror ? 'checked' : ''}> ${t('pt_mirror')}</label>
|
||
|
|
</div>
|
||
|
|
<div class="pt-row"><span>${t('pt_size')}</span><input type="range" id="ptSize" min="2" max="60" value="${size}">
|
||
|
|
<span id="ptExtra"></span></div>
|
||
|
|
<div class="pt-colors" id="ptColors">${COLORS.map(c => `<span class="sw" data-c="${c}" style="background:${c}"></span>`).join('')}<input type="color" id="ptPick" value="${color}"></div>
|
||
|
|
<div class="gbar"><button id="ptUndo">↶ ${t('pt_undo')}</button><button id="ptClear">${t('pt_clear')}</button><div class="grow"></div>
|
||
|
|
<button id="ptCancel">${t('cancel')}</button><button class="primary" id="ptDone">✓ ${t('pt_done')}</button></div>`;
|
||
|
|
$('shop').appendChild(panel);
|
||
|
|
const cv = $('ptCv');
|
||
|
|
cv.addEventListener('pointerdown', down);
|
||
|
|
cv.addEventListener('pointermove', move);
|
||
|
|
cv.addEventListener('pointerup', up);
|
||
|
|
cv.addEventListener('pointercancel', up);
|
||
|
|
panel.querySelectorAll('[data-tool]').forEach(b => { b.onclick = () => { tool = b.dataset.tool; opt.sfx.click(); refresh(); }; });
|
||
|
|
panel.querySelectorAll('#ptColors .sw').forEach(s => { s.onclick = () => { color = s.dataset.c; $('ptPick').value = color; opt.sfx.click(); refresh(); }; });
|
||
|
|
$('ptPick').oninput = e => { color = e.target.value; refresh(); };
|
||
|
|
$('ptSize').oninput = e => { size = +e.target.value; };
|
||
|
|
$('ptMirror').onchange = e => { mirror = e.target.checked; };
|
||
|
|
$('ptUndo').onclick = () => { if (undo.length) { lc.putImageData(undo.pop(), 0, 0); live(); opt.sfx.click(); } };
|
||
|
|
$('ptClear').onclick = () => { snap(); lc.clearRect(0, 0, LW, LH); live(); opt.sfx.click(); };
|
||
|
|
$('ptDone').onclick = () => { opt.sfx.buy(); close(); opt.onDone(); };
|
||
|
|
$('ptCancel').onclick = () => { lc.putImageData(before, 0, 0); opt.sfx.click(); close(); opt.onCancel(); };
|
||
|
|
panel.querySelectorAll('#ptView button').forEach(b => { b.onclick = () => {
|
||
|
|
panel.querySelectorAll('#ptView button').forEach(x => x.classList.toggle('on', x === b));
|
||
|
|
opt.view.face(b.dataset.v === 'free' ? null : b.dataset.v); opt.sfx.click();
|
||
|
|
}; });
|
||
|
|
// typing in the text field must not drive the robot / open menus
|
||
|
|
panel.addEventListener('keydown', e => e.stopPropagation());
|
||
|
|
}
|
||
|
|
|
||
|
|
function refresh() {
|
||
|
|
panel.querySelectorAll('[data-tool]').forEach(b => b.classList.toggle('on', b.dataset.tool === tool));
|
||
|
|
panel.querySelectorAll('#ptColors .sw').forEach(s => s.classList.toggle('on', s.dataset.c === color));
|
||
|
|
const ex = $('ptExtra');
|
||
|
|
if (tool === 'stamp') {
|
||
|
|
ex.innerHTML = STAMPS.map(s => `<button class="pt-st ${s === stamp ? 'on' : ''}" data-s="${s}">${s}</button>`).join('');
|
||
|
|
ex.querySelectorAll('button').forEach(b => { b.onclick = () => { stamp = b.dataset.s; refresh(); }; });
|
||
|
|
} else if (tool === 'text') {
|
||
|
|
ex.innerHTML = `<input type="text" id="ptText" maxlength="14" placeholder="${t('pt_text_ph')}" value="${text.replace(/"/g, '"')}">`;
|
||
|
|
$('ptText').oninput = e => { text = e.target.value; };
|
||
|
|
} else ex.innerHTML = '';
|
||
|
|
const p = opt.getPaint();
|
||
|
|
$('ptCv').style.backgroundColor = '#' + (p.body >>> 0).toString(16).padStart(6, '0');
|
||
|
|
}
|
||
|
|
|
||
|
|
function pos(e) {
|
||
|
|
const r = e.target.getBoundingClientRect();
|
||
|
|
return { x: (e.clientX - r.left) / r.width * LW, y: (e.clientY - r.top) / r.height * LH };
|
||
|
|
}
|
||
|
|
const halfOf = x => (x < LW / 2 ? 0 : 1);
|
||
|
|
function snap() { undo.push(lc.getImageData(0, 0, LW, LH)); if (undo.length > 25) undo.shift(); }
|
||
|
|
// every mark is clipped to the half it started in; in the top view "mirror" copies it to the other robot side
|
||
|
|
function withClip(half, fn) {
|
||
|
|
lc.save(); lc.beginPath(); lc.rect(half * LW / 2, 0, LW / 2, LH); lc.clip();
|
||
|
|
fn(false);
|
||
|
|
if (mirror && half === 0) { lc.translate(0, LH); lc.scale(1, -1); fn(true); }
|
||
|
|
lc.restore();
|
||
|
|
}
|
||
|
|
function down(e) {
|
||
|
|
e.preventDefault();
|
||
|
|
e.target.setPointerCapture(e.pointerId);
|
||
|
|
const p = pos(e), half = halfOf(p.x);
|
||
|
|
snap();
|
||
|
|
if (tool === 'stamp' || tool === 'text') {
|
||
|
|
const s = tool === 'stamp' ? stamp : text.trim();
|
||
|
|
if (s) withClip(half, flip => {
|
||
|
|
lc.save(); lc.translate(p.x, p.y); if (flip) lc.scale(1, -1);
|
||
|
|
lc.fillStyle = color; lc.font = `${tool === 'text' ? 'bold ' : ''}${size * (tool === 'text' ? 1.2 : 2)}px "Russo One", "Segoe UI Black", "Segoe UI Emoji", sans-serif`;
|
||
|
|
lc.textAlign = 'center'; lc.textBaseline = 'middle';
|
||
|
|
if (tool === 'text') { lc.lineWidth = Math.max(2, size / 6); lc.strokeStyle = 'rgba(0,0,0,.6)'; lc.strokeText(s, 0, 0); }
|
||
|
|
lc.fillText(s, 0, 0); lc.restore();
|
||
|
|
});
|
||
|
|
opt.sfx.click(); live(); return;
|
||
|
|
}
|
||
|
|
if (tool === 'fill') { // fill the whole half (top or side) with the colour
|
||
|
|
lc.save(); lc.fillStyle = color; lc.fillRect(half * LW / 2, 0, LW / 2, LH); lc.restore();
|
||
|
|
opt.sfx.click(); live(); return;
|
||
|
|
}
|
||
|
|
drawing = { half, last: p };
|
||
|
|
paint(p, p);
|
||
|
|
}
|
||
|
|
function move(e) {
|
||
|
|
if (!drawing) return;
|
||
|
|
const p = pos(e);
|
||
|
|
paint(drawing.last, p);
|
||
|
|
drawing.last = p;
|
||
|
|
}
|
||
|
|
function up() { drawing = null; }
|
||
|
|
function paint(a, b) {
|
||
|
|
withClip(drawing.half, () => {
|
||
|
|
lc.globalCompositeOperation = tool === 'erase' ? 'destination-out' : 'source-over';
|
||
|
|
if (tool === 'spray') {
|
||
|
|
lc.fillStyle = color;
|
||
|
|
const n = Math.ceil(size * 1.5);
|
||
|
|
for (let i = 0; i < n; i++) {
|
||
|
|
const ang = Math.random() * Math.PI * 2, r = Math.random() * size;
|
||
|
|
lc.globalAlpha = 0.35; lc.fillRect(b.x + Math.cos(ang) * r, b.y + Math.sin(ang) * r, 1.6, 1.6);
|
||
|
|
}
|
||
|
|
lc.globalAlpha = 1;
|
||
|
|
} else {
|
||
|
|
lc.strokeStyle = color; lc.lineWidth = size; lc.lineCap = 'round'; lc.lineJoin = 'round';
|
||
|
|
lc.beginPath(); lc.moveTo(a.x, a.y); lc.lineTo(b.x + 0.01, b.y); lc.stroke();
|
||
|
|
}
|
||
|
|
lc.globalCompositeOperation = 'source-over';
|
||
|
|
});
|
||
|
|
live();
|
||
|
|
}
|
||
|
|
// preview canvas + 3D robot, at most once per frame
|
||
|
|
function live() {
|
||
|
|
if (pending) return;
|
||
|
|
pending = true;
|
||
|
|
requestAnimationFrame(() => {
|
||
|
|
pending = false;
|
||
|
|
const v = $('ptCv').getContext('2d');
|
||
|
|
v.clearRect(0, 0, LW, LH); v.drawImage(layer, 0, 0);
|
||
|
|
opt.onLive();
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
function open() {
|
||
|
|
if (!panel) build();
|
||
|
|
before = lc.getImageData(0, 0, LW, LH);
|
||
|
|
undo = [];
|
||
|
|
panel.style.display = '';
|
||
|
|
$('sRight').style.display = 'none'; $('shop').classList.add('painting');
|
||
|
|
opt.view.paintCam(true);
|
||
|
|
refresh(); live();
|
||
|
|
}
|
||
|
|
function close() {
|
||
|
|
if (!panel) return;
|
||
|
|
panel.style.display = 'none';
|
||
|
|
$('sRight').style.display = ''; $('shop').classList.remove('painting');
|
||
|
|
opt.view.paintCam(false); opt.view.face(null);
|
||
|
|
}
|
||
|
|
// anything drawn at all? (decides stock stripes vs. own artwork on the model)
|
||
|
|
function hasArt() {
|
||
|
|
const d = lc.getImageData(0, 0, LW, LH).data;
|
||
|
|
for (let i = 3; i < d.length; i += 16) if (d[i] > 8) return true;
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
return { open, close, hasArt, get isOpen() { return !!panel && panel.style.display !== 'none'; } };
|
||
|
|
};
|
||
|
|
})(window);
|