Browse Source

fix: IJS.input.xy() — use canvasRef closure, not broken IJS.canvas ref

Root cause: inside createIJSAPI(), the returned object is anonymous.
IJS.input.xy() referenced IJS.canvas but IJS was not in scope
(not yet assigned). Fixed by using a canvasRef closure object
that both IJS.canvas and IJS.input.xy() share.

Co-Authored-By: Claude <noreply@anthropic.com>
nonvioce 3 months ago
parent
commit
996b574df4
1 changed files with 13 additions and 6 deletions
  1. 13 6
      app/src/main/assets/console.js

+ 13 - 6
app/src/main/assets/console.js

@@ -855,6 +855,8 @@ class SymjaConsole {
     
     createIJSAPI(pluginOutputs) {
         const self = this;
+        // 闭包引用 — IJS.input.xy() 需要访问 canvas 对象
+        const canvasRef = { _canvas: null, _logicalW: 600, _logicalH: 800 };
         return {
             // ── IJS.Symja 数学引擎 ─────────────────────────
             Symja: {
@@ -940,6 +942,10 @@ class SymjaConsole {
                     this._ctx.scale(dpr, dpr);
                     this._logicalW = cw;
                     this._logicalH = ch;
+                    // 同步给 input.xy() 的闭包引用
+                    canvasRef._canvas = c;
+                    canvasRef._logicalW = cw;
+                    canvasRef._logicalH = ch;
                     panel.appendChild(c);
                     return this._ctx;
                 },
@@ -969,6 +975,10 @@ class SymjaConsole {
                     if (this._canvas) {
                         this._canvas.width = w;
                         this._canvas.height = h;
+                        this._logicalW = w;
+                        this._logicalH = h;
+                        canvasRef._logicalW = w;
+                        canvasRef._logicalH = h;
                     }
                 },
                 // 动画循环
@@ -997,17 +1007,14 @@ class SymjaConsole {
                 onTouchEnd: function(cb) { this._callbacks.touchend = cb; this._bind(); },
                 /** 将触摸事件的 clientX/Y 转换为画布逻辑坐标 */
                 xy: function(e) {
-                    var idx = 0;
-                    if (e.changedTouches) idx = 0;
-                    else if (e.touches) idx = 0;
                     var touch = e.changedTouches ? e.changedTouches[0] : (e.touches ? e.touches[0] : e);
                     if (!touch) return {x:0,y:0};
-                    var c = IJS.canvas._canvas;
+                    var c = canvasRef._canvas;
                     if (!c) return {x: touch.clientX, y: touch.clientY};
                     var r = c.getBoundingClientRect();
                     return {
-                        x: (touch.clientX - r.left) * (IJS.canvas._logicalW / r.width),
-                        y: (touch.clientY - r.top) * (IJS.canvas._logicalH / r.height)
+                        x: (touch.clientX - r.left) * (canvasRef._logicalW / r.width),
+                        y: (touch.clientY - r.top) * (canvasRef._logicalH / r.height)
                     };
                 },
                 _bound: false,