亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Stencil js - requestAnimationFrame 并未出現在所有組件中

Stencil js - requestAnimationFrame 并未出現在所有組件中

慕桂英3389331 2023-08-18 17:08:44
我使用 stencil.js 創建了一個簡單的目錄項組件。在組件中有一個畫布標簽,我在其上繪制了動畫曲線。在 componentDidLoad 函數中,我定義了畫布,對其進行初始化并調用 animate 函數。這是組件本身的代碼:import { Component, Host, h, Prop, Element  } from "@stencil/core";import { Item } from "../hotb-catalog-container/data";import { initCanvas, init, animate } from "./wave.js";import axios from "axios";@Component({  tag: "hotb-catalog-item",  styleUrl: "hotb-catalog-item.scss",  shadow: false,})export class HotbCatalogItem {  @Prop() item: Item;  @Element() el: HTMLElement;  // @Event({ bubbles: true, composed: true }) itemSelect: EventEmitter<Item>;  itemSelected(event: Event) {    event.preventDefault();    //sessionStorage.setItem("item", JSON.stringify(this.item));    axios.post("/item", { item: this.item }).then(() => {      window.location.href = "http://localhost:3000/item";    });  }  componentDidLoad() {    let canvas = this.el.querySelector('canvas');    initCanvas(canvas);    init();    animate();  }  render() {    return (      <Host>        <div class="heading">          <h1>{this.item.name}</h1>          <span> ?{this.item.origin} &#183; {this.item.taste}</span>        </div>        <div class="center-part">          <div class="center-part__img"></div>          <div class="center-part__icon center-part__icon--temp">            {this.item.temp}&deg;C          </div>          <div class="center-part__icon center-part__icon--time">            {this.item.time}          </div>        </div>        <a          href="/item"          onClick={this.itemSelected.bind(this)}          class="primary-btn homepage__tea"        >          ?????        </a>        <canvas></canvas>      </Host>    );  }}最終結果是組件顯示畫布和線條,但動畫僅在其中之一中發生。其余的在初始狀態下是靜態的。請告訴我為什么會發生這種情況以及我可以采取哪些措施來修復它,以便所有組件都可以動畫化。需要注意的是,當我刷新代碼并且瀏覽器通過熱重載刷新時,另一個組件會在每次刷新時啟動動畫,依此類推。
查看完整描述

1 回答

?
慕容708150

TA貢獻1831條經驗 獲得超4個贊

問題是某些變量是在函數外部定義的,initCanvas因此在所有組件(line1、和)之間共享。因此,每次您調用時,它們都會被覆蓋。line2ctxcanvasEleminitCanvas


一個快速的解決方案是將其包裝在一個類中:


export class WaveCanvas {

  constructor(canvas) {

    this.canvasElem = canvas;

    this.ctx = canvas.getContext("2d");

    const parent = canvas.parentElement;

    canvas.width = parent.clientWidth;

    canvas.height = parent.scrollHeight;

  }


  init() {

    // ...

    this.line1 = new Line(...);

  }


  animate() {

    requestAnimationFrame(() => this.animate());

    this.ctx.clearRect(0, 0, this.canvasElem.width, this.canvasElem.height);


    this.line1.update();

    this.line2.update();

  }

}

然后在組件中實例化它:


  componentDidLoad() {

    let canvas = this.el.querySelector('canvas');

    const waveCanvas = new WaveCanvas(canvas);

    waveCanvas.init();

    waveCanvas.animate();

  }

這樣, 的每個實例都WaveCanvas將擁有自己對正確<canvas>元素的引用。


查看完整回答
反對 回復 2023-08-18
  • 1 回答
  • 0 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號