/    Sign up×
Community /Pin to ProfileBookmark

HTML5 Canvas Question

I am looking to create an element on a web page that will contain user-arrangeable icons, where the icons have a right-click context menu that can interact with each icon. I have tried using an HTML5 canvas and I have the icons arranged how I want them, but there is no facility to make each individual icon selectable or movable within the canvas by the user.

Does anyone have a suggestion or another way to accomplish this? I would be ok with the icons not being movable, but the icons need to be selectable.

Thank you!

to post a comment
HTML

7 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumSep 21.2021 — Movable icons can be made easily by use of the library jCanvas:

https://projects.calebevans.me/jcanvas/docs/draggableLayers/

jCanvas supports the event 'contextmenu' which is firing at rightclick.

What do you mean by "selectable"? It would be easy to change the color or draw a border around an icon in order to visualize that it's selected.
Copy linkTweet thisAlerts:
@coder_toaderauthorSep 21.2021 — @Sempervivum#1637338 This might be exactly what im looking for. Selectable just means if I right click on that icon I want edit or change properties of that icon.

Thank you! I will check this out momentarily.
Copy linkTweet thisAlerts:
@coder_toaderauthorSep 22.2021 — @Sempervivum#1637338 This seems to be really good but I cannot find the documentation on the contextmenu event. Would you happen to have some info? Google was not of much help.

Thank you!
Copy linkTweet thisAlerts:
@SempervivumSep 22.2021 — Unfortunately this is not documented in detail. Take a look at the click event

https://projects.calebevans.me/jcanvas/docs/mouseEvents/

and replace click by contextmenu. I tried it in the sandbox and it worked.
Copy linkTweet thisAlerts:
@coder_toaderauthorOct 08.2021 — @Sempervivum#1637480 Ok, I checked out the functionality and it works in the sandbox. I have also rebuilt my original canvas now using jCanvas. I'm still pretty new to most of this stuff. Any chance you might be able to share a good context menu resource?
Copy linkTweet thisAlerts:
@SempervivumOct 08.2021 — @coder_toader#1637965 Unfortunately I do not know.

In general there are two ways to go:
  • - Create the context menu in jCanvas. Pro: Straight forward, you would be sticking to one tool. Con: Might grow a bit complex to create and style.

  • - Create the context menu in HTML and CSS. Pro: Easier to create and style. Con: Mixing up two different tools.
  • Copy linkTweet thisAlerts:
    @boohooOct 09.2021 — Are you sure you want to do it on canvas? Keep in mind that canvas limits flexibility a whole lot, because it's just a matrix of pixels. And icons are usually better as vectors that are always razor sharp.

    And yes, you can do whatever you'd like on the right click:
    ``javascript<i>
    </i>const element = document.querySelector('.someElement'); // example
    element.addEventListener('contextmenu', event =&gt; {
    event.preventDefault();
    // do whatever you like
    });<i>
    </i>
    ``
    ×

    Success!

    Help @coder_toader spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.25,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...