#Can't manage to load an image in Tauri v2

29 messages · Page 1 of 1 (latest)

hoary relic
#

I followed the v2 docs and got here
https://beta.tauri.app/fr/references/v2/js/fs/#readbinaryfile
but it says that it can't find '@tauri-apps/plugin-fs'. I installed Tauri correctly, I added the path to the scope

    "fs": {
      "readFile": true,
      "readDir": true,
      "scope": [
        "../src/assets/spritesheets/*"
      ]
    }
  }```
why is this happening, am I missing something obvious..? Thanks! :)
Tauri

The cross-platform app building toolkit

spare harness
#

The scope you've set doesn't make sense to be honest.

hoary relic
#

Oh, should I specify the file extension?

spare harness
#

nono wait lemme explain, i didn't mean to press enter yet :D

hoary relic
#

Oh okay x)

#

No problem

spare harness
#

files in your frontend files are either copied into the binary itself or served by a dev server. In the first case you can't load them via the fs api and in the second one it doesn't reallyyy make sense (but it should generally work).
Instead you can load frontend assets via the browser's native fetch function assuming your frontend bundler copies them without changing the name of the files (most bundlers have a public dir for this).

The main actual problem with your scope here is that these file paths would be incorrect after you run tauri build because the paths/scopes are evaluated at runtime not build time at which point those files won't exist (at this path).

hoary relic
#

I think I understood pretty much how it works..?

#

Well, I shouldn't even need fetch I think. I'm just trying to get a sprite to make a little game, so I did let img = new Image(); img.src = '../src/assets/spritesheets/turtle_walk_strip5.png'; but it just won't show up. I even tried with readBinaryFile, that's why I had this problem with the fs plugin. And because the sprite won't show up, I thought it was a scope error as usual

spare harness
#

are you using a frontend bundler like vite?

#

If so, try using an actual import like import imgSrc from 'path/relative/to/src/because/thats/the/cwd'

hoary relic
#

Uhhh, just vanilla typescript, html and css. No framework or anything. Isn't vite the default bundler anyways?

spare harness
#

depends on how you created the app, but yes if you used create-tauri-app's typescript template then yes (the vanilla js template comes without it btw)

#

but yeah, then what i wrote should work

hoary relic
#

Let me try then!

#

So then I just do img.src = imgSrc; right? Because it says it can't find the module, it thinks it's a module now :')
Sorry for my suddenly poor knowledge ^^'

spare harness
#

yes

hoary relic
#

Uhhh, it still trying to find the module even tho it's just an image, just why..?
On the doc you provided, does the element needs to be an image?

#

And even if so, the src can't be found somehow

hoary relic
#

I found another way to get it. Thanks for your help still! :D

spare harness
#

how did you end up doing it?

hoary relic
#

<img src="mysource.png"/>

img = document.getElementById('img') as HTMLImageElement;```
#

I think that's just the normal way to do it, and I was just dumb x)

spare harness
#

maybe it's the normal way, yeah

#

but you'll have to confirm it works in build mode too

#

if the file is in the src dir vite will give it a random name or even omit it if it can't detect anything using it. To keep file names you can move them into the public dir

hoary relic
#

Oh, there is no public dir