第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Stencil js - requestAnimationFrame 并未出現(xiàn)在所有組件中

Stencil js - requestAnimationFrame 并未出現(xiàn)在所有組件中

慕桂英3389331 2023-08-18 17:08:44
我使用 stencil.js 創(chuàng)建了一個(gè)簡單的目錄項(xiàng)組件。在組件中有一個(gè)畫布標(biāo)簽,我在其上繪制了動(dòng)畫曲線。在 componentDidLoad 函數(shù)中,我定義了畫布,對其進(jìn)行初始化并調(diào)用 animate 函數(shù)。這是組件本身的代碼: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>    );  }}最終結(jié)果是組件顯示畫布和線條,但動(dòng)畫僅在其中之一中發(fā)生。其余的在初始狀態(tài)下是靜態(tài)的。請告訴我為什么會(huì)發(fā)生這種情況以及我可以采取哪些措施來修復(fù)它,以便所有組件都可以動(dòng)畫化。需要注意的是,當(dāng)我刷新代碼并且瀏覽器通過熱重載刷新時(shí),另一個(gè)組件會(huì)在每次刷新時(shí)啟動(dòng)動(dòng)畫,依此類推。
查看完整描述

1 回答

?
慕容708150

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個(gè)贊

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


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


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();

  }

}

然后在組件中實(shí)例化它:


  componentDidLoad() {

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

    const waveCanvas = new WaveCanvas(canvas);

    waveCanvas.init();

    waveCanvas.animate();

  }

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


查看完整回答
反對 回復(fù) 2023-08-18
  • 1 回答
  • 0 關(guān)注
  • 121 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號