You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

相同场景ID对象的Callback函数异常:仅后配置回调生效

问题描述

当多个对象使用相同场景ID时,Callback函数工作异常——只有最后配置的回调会被执行。这个问题通常不容易被发现,因为Callback的设置频率不高,但即使是不使用子场景的简单案例,也可能出现回调执行异常的情况,特此咨询确认。

重现命令

R -e "rmarkdown::render('ex06.Rmd', output_file='index.html')"

代码文件

cb06.Rmd

---
title: "cb06.Rmd"
output: html_document
vignette: >
  %\VignetteIndexEntry{Callback implementation}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include=FALSE}
library(knitr)
library(rgl)
options(rgl.useNULL = TRUE)
setupKnitr(autoprint = FALSE)
opts_chunk$set(echo=FALSE, error=FALSE, warning=FALSE, message=FALSE)
knit_hooks$set(webgl = hook_webgl)
setupKnitr(autoprint = FALSE)
sys.source("ex06.R", envir = knit_global())
```{r} func1() id1 <- subsceneInfo()$id

js1 <- readLines("cb06.txt")
js1 <- sub("%subid%", id1, js1)
js1 <- sub(".begin", ".begin1", js1)
js1 <- sub(".update", ".update1", js1)

scene <- setUserCallbacks("left",
begin = "begin1",
update = "update1",
subscene = id1,
applyToScene = TRUE,
applyToDev = FALSE,
javascript = js1)

rglwidget(scene, width = 0.4 * figWidth(), height = 0.4 * figWidth())

```{r}
func1()
id2 <- subsceneInfo()$id

js2 <- readLines("cb06.txt")
js2 <- sub("%subid%", id2, js2)
js2 <- sub(".begin", ".begin2", js2)
js2 <- sub(".update", ".update2", js2)

scene <- setUserCallbacks("left", 
      begin = "begin2",
      update = "update2",
      subscene = id2,
      applyToScene = TRUE,
      applyToDev = FALSE,
      javascript = js2)

rglwidget(scene, width = 0.4 * figWidth(), height = 0.4 * figWidth())
```

ex06.R

## ex06.R
func1 <- function() {
  open3d()
  subscene <- newSubscene3d(newviewport = c(0, 0, 500, 500))
  par3d(windowRect = c(0, 0, 500, 500))
  view3d(theta=0, phi=0, zoom=1, fov=0)
  bg3d(color = "grey")
  spheres3d(0,0,0, r=10)
  spheres3d(500,500,0, r=10)
}

cb06.txt

// cb06.txt
window.subid = %subid%;

window.begin = function(x, y) {
    let activeSub = this.getObj(subid);
    activeSub.userSaveMat = new CanvasMatrix4(activeSub.par3d.userMatrix);
    this.canvas.style.cursor = "grabbing";
};

window.update = function(x, y) { 
    let objects = this.scene.objects,
        activeSub = this.getObj(subid);

    activeSub = this.scene.objects[subid];
//  alert(subid)
    activeSub.par3d.userMatrix.load(objects[subid].userSaveMat);
    activeSub.par3d.userMatrix.rotate(x, 0, 1, 0);
    activeSub.par3d.userMatrix.rotate(y, -1, 0, 0);
    this.drawScene();
};

内容的提问来源于stack exchange,提问作者大根おろし

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 11:54:50