let wepo1=1;
let wepo2=2;
let wepo3=3;
let wepo=0;
function weaponselector {
}
document.querySelector("button[name='shotgun/parent']").addEventListener("click",function() {
weaponselector(wepo + wepo1);
});
document.querySelector("button[name='pistol']").addEventListener("click",function() {
weaponselector(wepo+wepo2);
});
document.querySelector("button[name='rifle']").addEventListener("click",function() {
weaponselector(wepo+wepo3);
if (weaponselector=1){
let weapon="shotgun" === {
weapondamage:20
};
}
if (weaponselector=2) {
let weapon="pistol" === {
weapondamage:5
};
}
if (weaponselector=3) {
let weapon="rifle" === {
weapondamage:10
};
}
<main>
<section class="center">
<h1>Hello!</h1>
<p>This is project page.</p>
</section>
<section id="canvas">
<canvas></canvas>
</section>
</main>
<script>
import { SVG } from '@svgdotjs/svg.js';
var draw = SVG().addTo("#canvas").size(300, 300)
var rect = draw.rect(100, 100).attr({ fill: '#f06' })
</script>
但它在浏览器控制台上打印出此错误:
TypeError: Cannot read properties of null (reading 'put')
at Svg.addTo (@svgdotjs_svg__js.js?t=1722923695034&v=5d11dd42:1846:32)
at instance (index.svelte:13:22)
at init (chunk-5CD65IUO.js?v=5d11dd42:2137:23)
at new Project (index.svelte:14:55)
at createComponent (svelte-hooks.js?v=5d11dd42:206:20)
at targetCmp.$replace (svelte-hooks.js?v=5d11dd42:269:15)
at refreshComponent (proxy.js?v=5d11dd42:171:15)
at ProxyAdapterDom.rerender (proxy-adapter-dom.js?v=5d11dd42:77:5)
at proxy.js?v=5d11dd42:408:9
at Array.forEach (<anonymous>)
看起来 Svg.addTo() 方法无法找到 targetDOM。我认为这是 DOM 加载时间的问题,因此我尝试了这个:
<main>
<section class="center">
<h1>Hello!</h1>
<p>This is project page.</p>
</section>
<section id="canvas">
<canvas></canvas>
</section>
</main>
<script>
import { SVG } from '@svgdotjs/svg.js';
document.addEventListener("DOMContentLoaded",()=>{
var draw = SVG().addTo("#canvas").size(300, 300)
var rect = draw.rect(100, 100).attr({ fill: '#f06' })
});
</script>