SPB Git

Repository Public

Canvas.tsx 5.2 KB 109 lines tsx

1/**2 * =============================================================================3 *                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : MacLustr Social Engine & Remote Navigator8 * File             : apps/navigator-web/src/viewer/Canvas.tsx9 * Module           : viewer10 * Repository       : https://git.spboucher.ai/maclustr-social11 * Project Website  : https://social.spboucher.ai12 * Created          : 2026-12-0113 * Last Modified    : 2026-12-0114 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// The remote view: a letterboxed <canvas> that only ever shows frames received from the worker,43// plus the visually hidden text area that receives keyboard input. No placeholder image is ever drawn.44import { forwardRef, useEffect, useImperativeHandle, useLayoutEffect, useRef, useState, type ReactNode } from 'react';45import { useT } from '../i18n.ts';46import type { View } from './input.ts';4748export type StageHandle = {49  draw(bitmap: ImageBitmap): void;50  stage: HTMLDivElement | null; canvas: HTMLCanvasElement | null; textarea: HTMLTextAreaElement | null;51};52type Props = {53  frame: { w: number; h: number } | null;54  view: View;55  cursor: string;56  interactive: boolean;57  keyboardFocused: boolean;58  /** Shown instead of the picture while no frame has arrived (the real reason, in words). */59  placeholder: ReactNode;60  /** Shown over the last received picture when it can no longer be trusted as live. */61  frozen: ReactNode;62  onSize(w: number, h: number): void;63  children?: ReactNode;64};6566export const Stage = forwardRef<StageHandle, Props>(function Stage({ frame, view, cursor, interactive, keyboardFocused, placeholder, frozen, onSize, children }, ref) {67  const t = useT();68  const stage = useRef<HTMLDivElement>(null), canvas = useRef<HTMLCanvasElement>(null), textarea = useRef<HTMLTextAreaElement>(null);69  const [box, setBox] = useState({ w: 0, h: 0 });70  const sizeCb = useRef(onSize); sizeCb.current = onSize;7172  useImperativeHandle(ref, () => ({73    draw(bitmap) {74      const c = canvas.current; if (!c) return;75      if (c.width !== bitmap.width || c.height !== bitmap.height) { c.width = bitmap.width; c.height = bitmap.height; }76      c.getContext('2d', { alpha: false })?.drawImage(bitmap, 0, 0);77    },78    get stage() { return stage.current; }, get canvas() { return canvas.current; }, get textarea() { return textarea.current; },79  }), []);8081  useLayoutEffect(() => {82    const el = stage.current; if (!el) return;83    const measure = () => { const w = el.clientWidth, h = el.clientHeight; setBox((b) => (b.w === w && b.h === h ? b : { w, h })); sizeCb.current(w, h); };84    measure();85    const ro = new ResizeObserver(measure); ro.observe(el);86    return () => ro.disconnect();87  }, []);88  // Once the picture is gone, the canvas is wiped: a stale image must not survive behind the placeholder.89  useEffect(() => { if (!frame) { const c = canvas.current; if (c) { c.width = 1; c.height = 1; } } }, [frame]);9091  const scale = frame && box.w > 0 && box.h > 0 ? Math.min(box.w / frame.w, box.h / frame.h) : 0;92  const cssW = frame ? Math.max(1, Math.floor(frame.w * scale)) : 0, cssH = frame ? Math.max(1, Math.floor(frame.h * scale)) : 0;93  const safeCursor = /^[a-z-]{1,24}$/.test(cursor) ? cursor : 'default';94  return (95    <div className={`stage-wrap${keyboardFocused ? ' kb' : ''}`}>96      <div ref={stage} className="stage" role="application" aria-label={t('viewer.stage_label')} aria-describedby="stage-help">97        <canvas ref={canvas} className={frame ? 'remote' : 'remote none'} aria-hidden="true"98          style={{ width: cssW, height: cssH, cursor: interactive ? safeCursor : 'default', transform: `translate(${view.tx}px, ${view.ty}px) scale(${view.z})` }} />99        {!frame && <div className="stage-msg">{placeholder}</div>}100        {frame && frozen && <div className="stage-frozen" role="status">{frozen}</div>}101      </div>102      <p id="stage-help" className="sr-only">{t('viewer.stage_help')}</p>103      <textarea ref={textarea} className="kb-proxy" aria-label={t('viewer.kb_label')} rows={1}104        autoCapitalize="off" autoCorrect="off" autoComplete="off" spellCheck={false} enterKeyHint="enter" />105      {children}106    </div>107  );108});109
Files main
Menu

Theme