{"id":35400,"date":"2025-05-04T13:50:17","date_gmt":"2025-05-04T05:50:17","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/35400.html"},"modified":"2025-05-04T13:50:17","modified_gmt":"2025-05-04T05:50:17","slug":"%e3%80%90harmonyos-next%e4%b9%8b%e6%97%85%e3%80%91%e5%9f%ba%e4%ba%8earkts%e5%bc%80%e5%8f%91%e4%b8%89-%ef%bc%9e-%e5%85%bc%e5%ae%b9js%e7%9a%84%e7%b1%bbweb%e5%bc%80%e5%8f%91%e4%b8%83-%ef%bc%9e-js","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/35400.html","title":{"rendered":"\u3010HarmonyOS Next\u4e4b\u65c5\u3011\u57fa\u4e8eArkTS\u5f00\u53d1(\u4e09) -\uff1e \u517c\u5bb9JS\u7684\u7c7bWeb\u5f00\u53d1(\u4e03) -\uff1e JS\u52a8\u753b(\u4e8c)"},"content":{"rendered":"<p id=\"main-toc\"><span style=\"color:#ed7976\">\u76ee\u5f55<\/span><\/p>\n<p id=\"1%20-%3E%20%E5%8A%A8%E7%94%BB%E5%8A%A8%E6%95%88-toc\" style=\"margin-left:0px\"><span style=\"color:#ed7976\">1 -&gt; \u52a8\u753b\u52a8\u6548<\/span><\/p>\n<p id=\"ZH-CN_TOPIC_0000001333321285__\u521b\u5efa\u52a8\u753b\u5bf9\u8c61-toc\" style=\"margin-left:40px\"><span style=\"color:#ed7976\">1.1 -&gt; \u521b\u5efa\u52a8\u753b\u5bf9\u8c61<\/span><\/p>\n<p id=\"ZH-CN_TOPIC_0000001333321285__\u6dfb\u52a0\u52a8\u753b\u4e8b\u4ef6\u548c\u8c03\u7528\u63a5\u53e3-toc\" style=\"margin-left:40px\"><span style=\"color:#ed7976\">1.2 -&gt; \u6dfb\u52a0\u52a8\u753b\u4e8b\u4ef6\u548c\u8c03\u7528\u63a5\u53e3<\/span><\/p>\n<p id=\"2%20-%3E%20%E5%8A%A8%E7%94%BB%E5%B8%A7-toc\" style=\"margin-left:0px\"><span style=\"color:#ed7976\">2 -&gt; \u52a8\u753b\u5e27<\/span><\/p>\n<p id=\"ZH-CN_TOPIC_0000001281201230__\u8bf7\u6c42\u52a8\u753b\u5e27-toc\" style=\"margin-left:40px\"><span style=\"color:#ed7976\">2.1 -&gt; \u8bf7\u6c42\u52a8\u753b\u5e27<\/span><\/p>\n<p id=\"ZH-CN_TOPIC_0000001281201230__\u53d6\u6d88\u52a8\u753b\u5e27-toc\" style=\"margin-left:40px\"><span style=\"color:#ed7976\">2.2 -&gt; \u53d6\u6d88\u52a8\u753b\u5e27<\/span><\/p>\n<hr id=\"hr-toc\" \/>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"859\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250504055015-681700179aa4c.png\" width=\"1161\" \/><\/p>\n<h2 id=\"1%20-%3E%20%E5%8A%A8%E7%94%BB%E5%8A%A8%E6%95%88\">1 -&gt; \u52a8\u753b\u52a8\u6548<\/h2>\n<p><span style=\"color:#6eaad7\">\u901a\u8fc7\u8bbe\u7f6e\u63d2\u503c\u5668\u6765\u5b9e\u73b0\u52a8\u753b\u6548\u679c\u3002<\/span><\/p>\n<p><span style=\"color:#ed7976\">\u8bf4\u660e<\/span><\/p>\n<p><span style=\"color:#ed7976\">\u4eceAPI Version 6 \u5f00\u59cb\u652f\u6301\u3002<\/span><\/p>\n<h3 id=\"ZH-CN_TOPIC_0000001333321285__\u521b\u5efa\u52a8\u753b\u5bf9\u8c61\">1.1 -&gt; \u521b\u5efa\u52a8\u753b\u5bf9\u8c61<\/h3>\n<p><span style=\"color:#6eaad7\">\u901a\u8fc7createAnimator\u521b\u5efa\u4e00\u4e2a\u52a8\u753b\u5bf9\u8c61&#xff0c;\u901a\u8fc7\u8bbe\u7f6e\u53c2\u6570options\u6765\u8bbe\u7f6e\u52a8\u753b\u7684\u5c5e\u6027\u3002<\/span><\/p>\n<p>&lt;!&#8211; test.hml &#8211;&gt;<br \/>\n&lt;div class&#061;&#034;container&#034;&gt;<br \/>\n  &lt;div style&#061;&#034;width: 300px;height: 300px;margin-top: 100px;background: linear-gradient(pink, purple);transform: translate({{translateVal}});&#034;&gt;<br \/>\n  &lt;\/div&gt;<br \/>\n  &lt;div class&#061;&#034;row&#034;&gt;<br \/>\n    &lt;button type&#061;&#034;capsule&#034; value&#061;&#034;play&#034; onclick&#061;&#034;playAnimation&#034;&gt;&lt;\/button&gt;<br \/>\n  &lt;\/div&gt;<br \/>\n&lt;\/div&gt;<br \/>\n\/* test.css *\/<br \/>\n.container {<br \/>\n  width:100%;<br \/>\n  height:100%;<br \/>\n  flex-direction: column;<br \/>\n  align-items: center;<br \/>\n  justify-content: center;<br \/>\n}<br \/>\nbutton{<br \/>\n  width: 200px;<br \/>\n}<br \/>\n.row{<br \/>\n  width: 65%;<br \/>\n  height: 100px;<br \/>\n  align-items: center;<br \/>\n  justify-content: space-between;<br \/>\n  margin-top: 50px;<br \/>\n  margin-left: 260px;<br \/>\n}<br \/>\n\/* test.js *\/<br \/>\nimport animator from &#039;&#064;ohos.animator&#039;;<br \/>\nexport default {<br \/>\n  data: {<br \/>\n    translateVal: 0,<br \/>\n    animation: null<br \/>\n  },<br \/>\n  onInit() {},<br \/>\n  onShow(){<br \/>\n    var options &#061; {<br \/>\n      duration: 3000,<br \/>\n      easing:&#034;friction&#034;,<br \/>\n      delay:&#034;1000&#034;,<br \/>\n      fill: &#039;forwards&#039;,<br \/>\n      direction:&#039;alternate&#039;,<br \/>\n      iterations: 2,<br \/>\n      begin: 0,<br \/>\n      end: 180<br \/>\n    };\/\/\u8bbe\u7f6e\u53c2\u6570<br \/>\n    this.animation &#061; animator.createAnimator(options)\/\/\u521b\u5efa\u52a8\u753b<br \/>\n  },<br \/>\n  playAnimation() {<br \/>\n    var _this &#061; this;<br \/>\n    this.animation.onframe &#061; function(value) {<br \/>\n      _this.translateVal&#061; value<br \/>\n    };<br \/>\n    this.animation.play();<br \/>\n  }<br \/>\n} <\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"330\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250504055016-681700186be9c.png\" width=\"430\" \/><\/p>\n<p><span style=\"color:#ed7976\">\u8bf4\u660e<\/span><\/p>\n<ul>\n<li>\n<p><span style=\"color:#ed7976\">\u4f7f\u7528createAnimator\u521b\u5efa\u52a8\u753b\u5bf9\u8c61\u65f6\u5fc5\u987b\u4f20\u5165options\u53c2\u6570\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#ed7976\">begin\u63d2\u503c\u8d77\u70b9&#xff0c;\u4e0d\u8bbe\u7f6e\u65f6\u9ed8\u8ba4\u4e3a0\u3002<\/span><\/p>\n<\/li>\n<li>\n<p><span style=\"color:#ed7976\">end\u63d2\u503c\u7ec8\u70b9&#xff0c;\u4e0d\u8bbe\u7f6e\u65f6\u9ed8\u8ba4\u4e3a1\u3002<\/span><\/p>\n<\/li>\n<\/ul>\n<h3 id=\"ZH-CN_TOPIC_0000001333321285__\u6dfb\u52a0\u52a8\u753b\u4e8b\u4ef6\u548c\u8c03\u7528\u63a5\u53e3\">1.2 -&gt; \u6dfb\u52a0\u52a8\u753b\u4e8b\u4ef6\u548c\u8c03\u7528\u63a5\u53e3<\/h3>\n<p><span style=\"color:#6eaad7\">animator\u652f\u6301\u4e8b\u4ef6\u548c\u63a5\u53e3&#xff0c;\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0frame\u3001cancel\u3001repeat\u3001finish\u4e8b\u4ef6\u548c\u8c03\u7528update\u3001play\u3001pause\u3001cancel\u3001reverse\u3001finish\u63a5\u53e3\u81ea\u5b9a\u4e49\u52a8\u753b\u6548\u679c\u3002animator\u652f\u6301\u7684\u4e8b\u4ef6\u548c\u63a5\u53e3\u5177\u4f53\u89c1\u52a8\u753b\u4e2d\u7684createAnimator\u3002<\/span><\/p>\n<p>&lt;!&#8211; test.hml &#8211;&gt;<br \/>\n&lt;div style&#061;&#034;flex-direction: column;align-items: center;width: 100%;height: 100%;&#034;&gt;<br \/>\n  &lt;div style&#061;&#034;width:200px;height: 200px;margin-top: 100px;background: linear-gradient(#b30d29, #dcac1b);<br \/>\n  transform: scale({{scaleVal}});&#034;&gt;&lt;\/div&gt;<br \/>\n  &lt;div style&#061;&#034;width: {{DivWidth}};height: {{DivHeight}};margin-top: 200px;<br \/>\n  background: linear-gradient(pink, purple);margin-top: 200px;transform:translateY({{translateVal}});&#034;&gt;<br \/>\n  &lt;\/div&gt;<br \/>\n  &lt;div class&#061;&#034;row&#034;&gt;<br \/>\n    &lt;button type&#061;&#034;capsule&#034; value&#061;&#034;play&#034; onclick&#061;&#034;playAnimation&#034;&gt;&lt;\/button&gt;<br \/>\n    &lt;button type&#061;&#034;capsule&#034; value&#061;&#034;update&#034; onclick&#061;&#034;updateAnimation&#034;&gt;&lt;\/button&gt;<br \/>\n  &lt;\/div&gt;<br \/>\n  &lt;div class&#061;&#034;row1&#034;&gt;<br \/>\n    &lt;button type&#061;&#034;capsule&#034; value&#061;&#034;pause&#034; onclick&#061;&#034;pauseAnimation&#034;&gt;&lt;\/button&gt;<br \/>\n    &lt;button type&#061;&#034;capsule&#034; value&#061;&#034;finish&#034; onclick&#061;&#034;finishAnimation&#034;&gt;&lt;\/button&gt;<br \/>\n  &lt;\/div&gt;<br \/>\n  &lt;div class&#061;&#034;row2&#034;&gt;<br \/>\n    &lt;button type&#061;&#034;capsule&#034; value&#061;&#034;cancel&#034; onclick&#061;&#034;cancelAnimation&#034;&gt;&lt;\/button&gt;<br \/>\n    &lt;button type&#061;&#034;capsule&#034; value&#061;&#034;reverse&#034; onclick&#061;&#034;reverseAnimation&#034;&gt;&lt;\/button&gt;<br \/>\n  &lt;\/div&gt;<br \/>\n&lt;\/div&gt;<br \/>\n\/* test.css *\/<br \/>\nbutton{<br \/>\n  width: 200px;<br \/>\n}<br \/>\n.row{<br \/>\n  width: 65%;<br \/>\n  height: 100px;<br \/>\n  align-items: center;<br \/>\n  justify-content: space-between;<br \/>\n  margin-top: 150px;<br \/>\n  position: fixed;<br \/>\n  top: 52%;<br \/>\n  left: 120px;<br \/>\n}<br \/>\n.row1{<br \/>\n  width: 65%;<br \/>\n  height: 100px;<br \/>\n  align-items: center;<br \/>\n  justify-content: space-between;<br \/>\n  margin-top: 120px;<br \/>\n  position: fixed;<br \/>\n  top: 65%;<br \/>\n  left: 120px;<br \/>\n}<br \/>\n.row2{<br \/>\n  width: 65%;<br \/>\n  height: 100px;<br \/>\n  align-items: center;<br \/>\n  justify-content: space-between;<br \/>\n  margin-top: 100px;<br \/>\n  position: fixed;<br \/>\n  top: 75%;<br \/>\n  left: 120px;<br \/>\n}<br \/>\n\/* test.js *\/<br \/>\nimport animator from &#039;&#064;ohos.animator&#039;;<br \/>\nimport prompt from &#039;&#064;system.prompt&#039;;<br \/>\nexport default {<br \/>\n  data: {<br \/>\n    scaleVal:1,<br \/>\n    DivWidth:200,<br \/>\n    DivHeight:200,<br \/>\n    translateVal:0,<br \/>\n    animation: null<br \/>\n  },<br \/>\n  onInit() {<br \/>\n    var options &#061; {<br \/>\n      duration: 3000,<br \/>\n      fill: &#039;forwards&#039;,<br \/>\n      begin: 200,<br \/>\n      end: 270<br \/>\n    };<br \/>\n    this.animation &#061; animator.createAnimator(options);<br \/>\n  },<br \/>\n  onShow() {<br \/>\n    var _this&#061; this;<br \/>\n    \/\/\u6dfb\u52a0\u52a8\u753b\u91cd\u653e\u4e8b\u4ef6<br \/>\n    this.animation.onrepeat &#061; function() {<br \/>\n      prompt.showToast({<br \/>\n        message: &#039;repeat&#039;<br \/>\n      });<br \/>\n      var repeatoptions &#061; {<br \/>\n        duration: 2000,<br \/>\n        iterations: 1,<br \/>\n         direction: &#039;alternate&#039;,<br \/>\n         begin: 180,<br \/>\n         end: 240<br \/>\n       };<br \/>\n        _this.animation.update(repeatoptions);<br \/>\n        _this.animation.play();<br \/>\n      };<br \/>\n  },<br \/>\n  playAnimation() {<br \/>\n    var _this&#061; this;<br \/>\n    \/\/\u6dfb\u52a0\u52a8\u753b\u9010\u5e27\u63d2\u503c\u56de\u8c03\u4e8b\u4ef6<br \/>\n    this.animation.onframe &#061; function(value) {<br \/>\n      _this. scaleVal&#061; value\/150,<br \/>\n      _this.DivWidth &#061; value,<br \/>\n      _this.DivHeight &#061; value,<br \/>\n      _this.translateVal &#061; value-180<br \/>\n    };<br \/>\n    this.animation.play();<br \/>\n  },<br \/>\n  updateAnimation() {<br \/>\n    var newoptions &#061; {<br \/>\n      duration: 5000,<br \/>\n      iterations: 2,<br \/>\n      begin: 120,<br \/>\n      end: 180<br \/>\n    };<br \/>\n    this.animation.update(newoptions);<br \/>\n    this.animation.play();\/\/\u8c03\u7528\u52a8\u753b\u64ad\u653e\u63a5\u53e3<br \/>\n  },<br \/>\n  pauseAnimation() {<br \/>\n    this.animation.pause();\/\/\u8c03\u7528\u52a8\u753b\u6682\u505c\u63a5\u53e3<br \/>\n  },<br \/>\n  finishAnimation() {<br \/>\n    var _this&#061; this;<br \/>\n   \/\/\u6dfb\u52a0\u52a8\u753b\u5b8c\u6210\u4e8b\u4ef6<br \/>\n    this.animation.onfinish &#061; function() {<br \/>\n      prompt.showToast({<br \/>\n        message: &#039;finish&#039;<br \/>\n      })<br \/>\n    };<br \/>\n    this.animation.finish(); \/\/\u8c03\u7528\u52a8\u753b\u5b8c\u6210\u63a5\u53e3<br \/>\n  },<br \/>\n  cancelAnimation() {<br \/>\n    this.animation.cancel(); \/\/\u8c03\u7528\u52a8\u753b\u53d6\u6d88\u63a5\u53e3<br \/>\n  },<br \/>\n  reverseAnimation() {<br \/>\n    this.animation.reverse(); \/\/\u8c03\u7528\u52a8\u753b\u5012\u653e\u63a5\u53e3<br \/>\n  }<br \/>\n} <\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"714\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250504055016-681700187b467.png\" width=\"385\" \/><\/p>\n<p><span style=\"color:#ed7976\">\u8bf4\u660e<\/span><\/p>\n<p><span style=\"color:#ed7976\">\u5728\u8c03\u7528update\u63a5\u53e3\u7684\u8fc7\u7a0b\u4e2d\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u63a5\u53e3\u66f4\u65b0\u52a8\u753b\u53c2\u6570&#xff0c;\u5165\u53c2\u4e0ecreateAnimator\u4e00\u81f4\u3002<\/span><\/p>\n<h2 id=\"2%20-%3E%20%E5%8A%A8%E7%94%BB%E5%B8%A7\">2 -&gt; \u52a8\u753b\u5e27<\/h2>\n<h3 id=\"ZH-CN_TOPIC_0000001281201230__\u8bf7\u6c42\u52a8\u753b\u5e27\">2.1 -&gt; \u8bf7\u6c42\u52a8\u753b\u5e27<\/h3>\n<p><span style=\"color:#6eaad7\">\u8bf7\u6c42\u52a8\u753b\u5e27\u65f6\u901a\u8fc7requestAnimationFrame\u51fd\u6570\u9010\u5e27\u56de\u8c03&#xff0c;\u5728\u8c03\u7528\u8be5\u51fd\u6570\u65f6\u4f20\u5165\u4e00\u4e2a\u56de\u8c03\u51fd\u6570\u3002<\/span><\/p>\n<p><span style=\"color:#6eaad7\">runframe\u5728\u8c03\u7528requestAnimationFrame\u65f6\u4f20\u5165\u5e26\u6709timestamp\u53c2\u6570\u7684\u56de\u8c03\u51fd\u6570step&#xff0c;\u5c06step\u4e2d\u7684timestamp\u8d4b\u4e88\u8d77\u59cb\u7684startTime\u3002\u5f53timestamp\u4e0estartTime\u7684\u5dee\u503c\u5c0f\u4e8e\u89c4\u5b9a\u7684\u65f6\u95f4\u65f6\u5c06\u518d\u6b21\u8c03\u7528requestAnimationFrame&#xff0c;\u6700\u7ec8\u52a8\u753b\u5c06\u4f1a\u505c\u6b62\u3002<\/span><\/p>\n<p>&lt;!&#8211; test.hml &#8211;&gt;<br \/>\n&lt;div class&#061;&#034;container&#034;&gt;<br \/>\n  &lt;tabs onchange&#061;&#034;changecontent&#034;&gt;<br \/>\n    &lt;tab-content&gt;<br \/>\n      &lt;div class&#061;&#034;container&#034;&gt;<br \/>\n        &lt;stack style&#061;&#034;width: 300px;height: 300px;margin-top: 100px;margin-bottom: 100px;&#034;&gt;<br \/>\n          &lt;canvas id&#061;&#034;mycanvas&#034; style&#061;&#034;width: 100%;height: 100%;background-color: coral;&#034;&gt;<br \/>\n          &lt;\/canvas&gt;<br \/>\n          &lt;div style&#061;&#034;width: 50px;height: 50px;border-radius: 25px;background-color: indigo;position: absolute;left: {{left}};top: {{top}};&#034;&gt;<br \/>\n          &lt;\/div&gt;<br \/>\n        &lt;\/stack&gt;<br \/>\n        &lt;button type&#061;&#034;capsule&#034; value&#061;&#034;play&#034; onclick&#061;&#034;runframe&#034;&gt;&lt;\/button&gt;<br \/>\n      &lt;\/div&gt;<br \/>\n    &lt;\/tab-content&gt;<br \/>\n  &lt;\/tabs&gt;<br \/>\n&lt;\/div&gt;<br \/>\n\/* test.css *\/<br \/>\n.container {<br \/>\n  flex-direction: column;<br \/>\n  justify-content: center;<br \/>\n  align-items: center;<br \/>\n  width: 100%;<br \/>\n  height: 100%;<br \/>\n}<br \/>\nbutton{<br \/>\n  width: 300px;<br \/>\n}<br \/>\n\/* test.js *\/<br \/>\nexport default {<br \/>\n  data: {<br \/>\n    timer: null,<br \/>\n    left: 0,<br \/>\n    top: 0,<br \/>\n    flag: true,<br \/>\n    animation: null,<br \/>\n    startTime: 0,<br \/>\n  },<br \/>\n  onShow() {<br \/>\n    var test &#061; this.$element(&#034;mycanvas&#034;);<br \/>\n    var ctx &#061; test.getContext(&#034;2d&#034;);<br \/>\n    ctx.beginPath();<br \/>\n    ctx.moveTo(0, 0);<br \/>\n    ctx.lineTo(300, 300);<br \/>\n    ctx.lineWidth &#061; 5;<br \/>\n    ctx.strokeStyle &#061; &#034;red&#034;;<br \/>\n    ctx.stroke();<br \/>\n  },<br \/>\n  runframe() {<br \/>\n    this.left &#061; 0;<br \/>\n    this.top &#061; 0;<br \/>\n    this.flag &#061; true;<br \/>\n    this.animation &#061; requestAnimationFrame(this.step);<br \/>\n  },<br \/>\n  step(timestamp) {<br \/>\n    if (this.flag) {<br \/>\n      this.left &#043;&#061; 5;<br \/>\n      this.top &#043;&#061; 5;<br \/>\n      if (this.startTime &#061;&#061; 0) {<br \/>\n        this.startTime &#061; timestamp;<br \/>\n      }<br \/>\n      var elapsed &#061; timestamp &#8211; this.startTime;<br \/>\n        if (elapsed &lt; 500) {<br \/>\n          console.log(&#039;callback step timestamp: &#039; &#043; timestamp);<br \/>\n          this.animation &#061; requestAnimationFrame(this.step);<br \/>\n        }<br \/>\n      } else {<br \/>\n        this.left -&#061; 5;<br \/>\n        this.top -&#061; 5;<br \/>\n        this.animation &#061; requestAnimationFrame(this.step);<br \/>\n      }<br \/>\n      if (this.left &#061;&#061; 250 || this.left &#061;&#061; 0) {<br \/>\n        this.flag &#061; !this.flag<br \/>\n     }<br \/>\n    },<br \/>\n    onDestroy() {<br \/>\n      cancelAnimationFrame(this.animation);<br \/>\n    }<br \/>\n} <\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"373\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250504055016-681700188bc7c.png\" width=\"403\" \/><\/p>\n<p><span style=\"color:#ed7976\">\u8bf4\u660e<\/span><\/p>\n<p><span style=\"color:#ed7976\">requestAnimationFrame\u51fd\u6570\u5728\u8c03\u7528\u56de\u8c03\u51fd\u6570\u65f6\u5728\u7b2c\u4e00\u4e2a\u53c2\u6570\u4f4d\u7f6e\u4f20\u5165timestamp\u65f6\u95f4\u6233&#xff0c;\u8868\u793arequestAnimationFrame\u5f00\u59cb\u53bb\u6267\u884c\u56de\u8c03\u51fd\u6570\u7684\u65f6\u523b\u3002<\/span><\/p>\n<h3 id=\"ZH-CN_TOPIC_0000001281201230__\u53d6\u6d88\u52a8\u753b\u5e27\">2.2 -&gt; \u53d6\u6d88\u52a8\u753b\u5e27<\/h3>\n<p><span style=\"color:#6eaad7\">\u901a\u8fc7cancelAnimationFrame\u51fd\u6570\u53d6\u6d88\u9010\u5e27\u56de\u8c03&#xff0c;\u5728\u8c03\u7528cancelAnimationFrame\u51fd\u6570\u65f6\u53d6\u6d88requestAnimationFrame\u51fd\u6570\u7684\u8bf7\u6c42\u3002<\/span><\/p>\n<p>&lt;!&#8211; test.hml &#8211;&gt;<br \/>\n&lt;div class&#061;&#034;container&#034;&gt;<br \/>\n  &lt;tabs onchange&#061;&#034;changecontent&#034;&gt;<br \/>\n    &lt;tab-content&gt;<br \/>\n      &lt;div class&#061;&#034;container&#034;&gt;<br \/>\n        &lt;stack style&#061;&#034;width: 300px;height: 300px;margin-top: 100px;margin-bottom: 100px;&#034;&gt;<br \/>\n          &lt;canvas id&#061;&#034;mycanvas&#034; style&#061;&#034;width: 100%;height: 100%;background-color: coral;&#034;&gt;<br \/>\n          &lt;\/canvas&gt;<br \/>\n          &lt;div style&#061;&#034;width: 50px;height: 50px;border-radius: 25px;background-color: indigo;position: absolute;left: {{left}};top: {{top}};&#034;&gt;<br \/>\n          &lt;\/div&gt;<br \/>\n        &lt;\/stack&gt;<br \/>\n        &lt;button type&#061;&#034;capsule&#034; value&#061;&#034;play&#034; onclick&#061;&#034;runframe&#034;&gt;&lt;\/button&gt;<br \/>\n      &lt;\/div&gt;<br \/>\n    &lt;\/tab-content&gt;<br \/>\n  &lt;\/tabs&gt;<br \/>\n&lt;\/div&gt;<br \/>\n\/* test.css *\/<br \/>\n.container {<br \/>\n  flex-direction: column;<br \/>\n  justify-content: center;<br \/>\n  align-items: center;<br \/>\n  width: 100%;<br \/>\n  height: 100%;<br \/>\n}<br \/>\nbutton{<br \/>\n  width: 300px;<br \/>\n}<br \/>\n\/* test.js *\/<br \/>\nexport default {<br \/>\n  data: {<br \/>\n    timer: null,<br \/>\n    left: 0,<br \/>\n    top: 0,<br \/>\n    flag: true,<br \/>\n    animation: null<br \/>\n  },<br \/>\n  onShow() {<br \/>\n    var test &#061; this.$element(&#034;mycanvas&#034;);<br \/>\n    var ctx &#061; test.getContext(&#034;2d&#034;);<br \/>\n    ctx.beginPath();<br \/>\n    ctx.moveTo(0, 0);<br \/>\n    ctx.lineTo(300, 300);<br \/>\n    ctx.lineWidth &#061; 5;<br \/>\n    ctx.strokeStyle &#061; &#034;red&#034;;<br \/>\n    ctx.stroke();<br \/>\n  },<br \/>\n  runframe() {<br \/>\n    this.left &#061; 0;<br \/>\n    this.top &#061; 0;<br \/>\n    this.flag &#061; true;<br \/>\n    this.animation &#061; requestAnimationFrame(this.step);<br \/>\n  },<br \/>\n  step(timestamp) {<br \/>\n    if (this.flag) {<br \/>\n      this.left &#043;&#061; 5;<br \/>\n      this.top &#043;&#061; 5;<br \/>\n      this.animation &#061; requestAnimationFrame(this.step);<br \/>\n    } else {<br \/>\n      this.left -&#061; 5;<br \/>\n      this.top -&#061; 5;<br \/>\n      this.animation &#061; requestAnimationFrame(this.step);<br \/>\n    }<br \/>\n    if (this.left &#061;&#061; 250 || this.left &#061;&#061; 0) {<br \/>\n      this.flag &#061; !this.flag<br \/>\n    }<br \/>\n  },<br \/>\n  onDestroy() {<br \/>\n    cancelAnimationFrame(this.animation);<br \/>\n  }<br \/>\n} <\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"342\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250504055016-681700189bfdc.png\" width=\"384\" \/><\/p>\n<p><span style=\"color:#ed7976\">\u8bf4\u660e<\/span><\/p>\n<p><span style=\"color:#ed7976\">\u5728\u8c03\u7528\u8be5\u51fd\u6570\u65f6\u9700\u4f20\u5165\u4e00\u4e2a\u5177\u6709\u6807\u8bc6id\u7684\u53c2\u6570\u3002<\/span><\/p>\n<hr \/>\n<\/p>\n<p style=\"text-align:center\"><span style=\"color:#dad5e9\">\u611f\u8c22\u5404\u4f4d\u5927\u4f6c\u652f\u6301&#xff01;&#xff01;&#xff01;<\/span><\/p>\n<p style=\"text-align:center\"><span style=\"color:#dad5e9\">\u4e92\u4e09\u5566&#xff01;&#xff01;&#xff01;<\/span><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb3.4k\u6b21\uff0c\u70b9\u8d5e81\u6b21\uff0c\u6536\u85cf77\u6b21\u3002\u3010HarmonyOS\u4e4b\u65c5\u3011\u57fa\u4e8eArkTS\u5f00\u53d1(\u4e09) -\uff1e \u517c\u5bb9JS\u7684\u7c7bWeb\u5f00\u53d1(\u4e03) -\uff1e JS\u52a8\u753b(\u4e8c)<\/p>\n","protected":false},"author":2,"featured_media":35395,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[280,3030,187,87,67,190],"topic":[],"class_list":["post-35400","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-harmonyos","tag-harmonyos-next","tag-javascript","tag-87","tag-67","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010HarmonyOS Next\u4e4b\u65c5\u3011\u57fa\u4e8eArkTS\u5f00\u53d1(\u4e09) -\uff1e \u517c\u5bb9JS\u7684\u7c7bWeb\u5f00\u53d1(\u4e03) -\uff1e JS\u52a8\u753b(\u4e8c) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/35400.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010HarmonyOS Next\u4e4b\u65c5\u3011\u57fa\u4e8eArkTS\u5f00\u53d1(\u4e09) -\uff1e \u517c\u5bb9JS\u7684\u7c7bWeb\u5f00\u53d1(\u4e03) -\uff1e JS\u52a8\u753b(\u4e8c) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb3.4k\u6b21\uff0c\u70b9\u8d5e81\u6b21\uff0c\u6536\u85cf77\u6b21\u3002\u3010HarmonyOS\u4e4b\u65c5\u3011\u57fa\u4e8eArkTS\u5f00\u53d1(\u4e09) -\uff1e \u517c\u5bb9JS\u7684\u7c7bWeb\u5f00\u53d1(\u4e03) -\uff1e JS\u52a8\u753b(\u4e8c)\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/35400.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-05-04T05:50:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250504055015-681700179aa4c.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/35400.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/35400.html\",\"name\":\"\u3010HarmonyOS Next\u4e4b\u65c5\u3011\u57fa\u4e8eArkTS\u5f00\u53d1(\u4e09) -\uff1e \u517c\u5bb9JS\u7684\u7c7bWeb\u5f00\u53d1(\u4e03) -\uff1e JS\u52a8\u753b(\u4e8c) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-05-04T05:50:17+00:00\",\"dateModified\":\"2025-05-04T05:50:17+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/35400.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/35400.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/35400.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010HarmonyOS Next\u4e4b\u65c5\u3011\u57fa\u4e8eArkTS\u5f00\u53d1(\u4e09) -\uff1e \u517c\u5bb9JS\u7684\u7c7bWeb\u5f00\u53d1(\u4e03) -\uff1e JS\u52a8\u753b(\u4e8c)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u3010HarmonyOS Next\u4e4b\u65c5\u3011\u57fa\u4e8eArkTS\u5f00\u53d1(\u4e09) -\uff1e \u517c\u5bb9JS\u7684\u7c7bWeb\u5f00\u53d1(\u4e03) -\uff1e JS\u52a8\u753b(\u4e8c) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/35400.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010HarmonyOS Next\u4e4b\u65c5\u3011\u57fa\u4e8eArkTS\u5f00\u53d1(\u4e09) -\uff1e \u517c\u5bb9JS\u7684\u7c7bWeb\u5f00\u53d1(\u4e03) -\uff1e JS\u52a8\u753b(\u4e8c) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb3.4k\u6b21\uff0c\u70b9\u8d5e81\u6b21\uff0c\u6536\u85cf77\u6b21\u3002\u3010HarmonyOS\u4e4b\u65c5\u3011\u57fa\u4e8eArkTS\u5f00\u53d1(\u4e09) -\uff1e \u517c\u5bb9JS\u7684\u7c7bWeb\u5f00\u53d1(\u4e03) -\uff1e JS\u52a8\u753b(\u4e8c)","og_url":"https:\/\/www.wsisp.com\/helps\/35400.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-05-04T05:50:17+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/05\/20250504055015-681700179aa4c.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"5 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/35400.html","url":"https:\/\/www.wsisp.com\/helps\/35400.html","name":"\u3010HarmonyOS Next\u4e4b\u65c5\u3011\u57fa\u4e8eArkTS\u5f00\u53d1(\u4e09) -\uff1e \u517c\u5bb9JS\u7684\u7c7bWeb\u5f00\u53d1(\u4e03) -\uff1e JS\u52a8\u753b(\u4e8c) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-05-04T05:50:17+00:00","dateModified":"2025-05-04T05:50:17+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/35400.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/35400.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/35400.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010HarmonyOS Next\u4e4b\u65c5\u3011\u57fa\u4e8eArkTS\u5f00\u53d1(\u4e09) -\uff1e \u517c\u5bb9JS\u7684\u7c7bWeb\u5f00\u53d1(\u4e03) -\uff1e JS\u52a8\u753b(\u4e8c)"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/35400","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=35400"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/35400\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/35395"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=35400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=35400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=35400"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=35400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}