JS / Webpack: an SVG graphic (map via D3.js) is not rendering when compiling via webpack (src included)

huangapple go评论56阅读模式
英文:

JS / Webpack: an SVG graphic (map via D3.js) is not rendering when compiling via webpack (src included)

问题

我正在使用Webpack制作一个包含自定义地图投影的网站,使用D3。尽管代码没有问题,但地图本身不显示。

我已经按照这个教程的步骤进行操作,甚至完全复制了演示的CodePen。多次检查了所有代码,甚至请Chat GPT帮助调试。然而,当我编译它时,地图根本不显示,只留下一个空白屏幕。

我最好的猜测是Webpack正在进行一些奇怪的编译/干扰,导致D3出现问题,或者有一些奇怪的东西阻止了SVG图形的渲染。检查它时,D3似乎可以正常生成SVG线条。

请注意,在canvas.js中调用ShowDemoMap()确实会加载并显示一个示例地图图像,确认Webpack实际上正在编译。只是没有显示D3地图。

非常感谢任何帮助,谢谢。

英文:

I'm using Webpack to make a website that contains a custom map projection using D3. Even though the code checks out, the map itself doesn't render.

I've followed this tutorial and even copied the demo CodePen to the letter. Triple-checked all the code, and even asked Chat GPT to help debug. However, when I compile it, the map simply doesn't render, leaving me with a blank screen.

My best guess is that Webpack is doing some weird compilation/interference that's breaking D3, or something funky that's preventing SVG graphics from rendering. Inspecting it, D3 seems to be generating the SVG lines fine though.

Note that calling ShowDemoMap() in canvas.js DOES load+show an example map image, confirming that webpack is actually compiling. It's just not showing the D3 map.

Any help extremely appreciated, cheers

答案1

得分: 0

I figured it out! Almost certainly because I was mis-assigning an element while selecting D3 elements. The main issue line was:

var joined = d3.select('#container').select('g.map')

probably due to some issue with dynamically creating the hierarchy in JS. Eg, createElement for container, createElementNS for SVGs, etc. Not sure what the issue was exactly (if you want to see the broken code, it's here - kudos if you can point out what I messed up, as I'm still not 100% sure) but at least now it's fixed.

Instead of creating the elements via JS and then embedding D3 into them, I just let D3 create the elements directly. SVG rendering was probably working all along, just some hierarchy assignment put it in the wrong spot.

If anybody ever comes across this in the future... hope this helps <3

英文:

Figured it out! Almost certainly because I was mis-assigning an element while selecting D3 elements. The main issue line was:

var joined = d3.select(&#39;#container&#39;).select(&#39;g.map&#39;)

probably due to some issue with dynamically creating the hierarchy in JS. Eg, createElement for container, createElementNS for SVGs, etc. Not sure what the issue was exactly (if you want to see the broken code, it's here - kudos if you can point out what I messed up, as I'm still not 100% sure) but at least now it's fixed.

Instead of creating the elements via JS and then embedding D3 into them, I just let D3 create the elements directly. SVG rendering was probably working all along, just some hierarchy assignment put it in the wrong spot.

If anybody ever comes across this in the future... hope this helps <3

huangapple
  • 本文由 发表于 2023年5月21日 07:01:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297646.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定