English 中文(简体)
直接在 Svelte 中的图像上绘制
原标题:Draw directly on an image in Svelte
  • 时间:2024-07-03 14:46:54
  •  标签:
  • svelte
I have an application that presents images. I need to let the user to click the image and to mark the specific place where the user clicked. Below listed a simple example of the current code. Clicking the image shows the relative coordinates of the click to image, i.e., as if the top-left corner of the image is the (0, 0).
BlankhandleMousemove(e)} />
The mouse position is ({mouseClickP.x}, {mouseClickP.y})
I need to mark the point clicked with a red dot, so that it will be seen with a naked eye. I saw this answer, but it uses a canvas which I could not understand how to make it work with an image, as I need the point to appear directly on the image itself.
问题回答
You can use an HTML structure like this one:
The idea is that: .container is equally big as .image .marker can be positioned anywhere within .container To make (1) happen, use the following CSS: .container{ display: inline-block; } To make (2) happen, use the additional CSS: .container{ /* This enables children to use position: absolute to be positioned using absolute coordinates relative to this element. */ position: relative; } .marker{ /* Here we position this element relative to the .container element. */ position: absolute; left: 25%; /* Update this using JS when the user clicks. */ top: 75%; /* Update this using JS when the user clicks. */ /* left and top indicates the position of the top left corner. You probably want it to be centered at that position. So translate (move it) 50% of it s size up and to the left. */ transform: translate(-50%, -50%); /* Make it look like a circle. */ width: 50px; aspect-ratio: 1; border-radius: 50%; background-color: red; } What remains if for you to write the JS to only show the .marker after the user has clicked the image, and to update it s left and top values.




相关问题
Rollup: Change the output destination of an input html file

While building I wish to change the output destination to an input file when using rollup+vite. An Example MVE: Input src/ | foo/ | | foo.html | bar | | bar.css Desired Output dist/ | foo.html | ...

ckeditor 5 plugins not working with svelte

I m trying to get ckeditor with html support to work within svelte. <script> import { onMount } from svelte ; import ClassicEditor from @ckeditor/ckeditor5-build-classic ; import {...

vite building issue for svelte-kit

Everytime I run : npm run dev in my terminal, I get this error: > enhanzer@0.0.1 dev > vite dev Forced re-optimization of dependencies Error: Failed to scan for dependencies from entries: C:...

TanStack Svelte Table Pagination

I was using TanStack Table Svelte Library and I wanted to use pagination but they haven t given any example on that, if anyone has any idea how to use the pagination on the Svelte table or if there ...

热门标签