1

I'm doing a plant growth simulator to teach kids about growing plants. There are threads about changing the pointer such as these ones: JavaScript how to change mouse cursor to an image? How to change the mouse pointer to an Image when clicked on it in c#

However, the concerns are different. What I want to do is have the mouse pointer itself change to the image that was clicked on so it will give something like this:

  1. click on the shovel in the sidebar and the mouse pointer changes to a shovel
  2. then the kids click in the place where they would like to dig a hole for their plant and the image of dirt mound will appear where they clicked
  3. they go back to the sidebar and click on the seeds and the mouse pointer will change from a shovel to a bag of seeds

I don't know if I should be trying to change the pointer on a click event or make the pointer invisible and append a copy of the selected image to it so I would appreciate some help on that.

  • Please Show us code which you have tried.. [Please Read How to Ask Question](https://stackoverflow.com/help/how-to-ask) – Krunal Shah May 02 '18 at 04:33
  • @CertainPerformance I had looked up the question before and it wasn't exactly the same thing from my perception. I edited the original question to indicate this. – user9192911 May 02 '18 at 05:17

2 Answers2

0

in JS, just do:

document.body.style.cursor = "url([url]), pointer"

for example:

document.body.style.cursor = "url(http://bringerp.free.fr/Files/RotMG/cursor.gif), pointer"

To replace to default cursor, change cursor style to empty:

document.body.style.cursor = ""
Drakinite
  • 363
  • 1
  • 13
  • I tried your suggestion and it worked (https://jsfiddle.net/r9Lbmhxv/) but it's not quite that. What I mean is I start with the default mouse pointer, then when I click on the top box the pointer changes to the box and I no longer see the arrow. I hope that's more clear. I'm inserting a link to similar garden game as an example: http://www.girlgames4u.com/vegetable-garden-game.html (it requires Flash, sorry) – user9192911 May 02 '18 at 05:41
  • Best bet is to set the cursor as the image of the shovel. Despite my best efforts, when dragging the html box, I couldn't get that damn "not allowed" cursor to go away. ( https://i.imgur.com/VXo6K1D.png ). Just use mousedown and mouseup events to control it. – Drakinite May 02 '18 at 05:57
0

To change the mouse cursor on demand, you'll have to add a click listener to the element in question. For example:

const src = 'https://www.gravatar.com/avatar/2f2eaccac43433e0bdd0b9f795286423?s=32&d=identicon&r=PG&f=1';
const docStyle = document.body.style;
document.querySelector('img').addEventListener('click', () => {
  if (!docStyle.cursor) docStyle.cursor = `url('${src}'), default`;
  else docStyle.cursor = null;
});
div {
  height: 300px;
  background-color: green;
}
<div>
  <img src="https://www.gravatar.com/avatar/2f2eaccac43433e0bdd0b9f795286423?s=32&d=identicon&r=PG&f=1">
</div>
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • Yes. Tks. I applied it to the original example and it worked fine (https://jsfiddle.net/r9Lbmhxv/2/) to ensure I understood. Then, I tried doing the same thing with a local image but it doesn't work. `const src = 'images/Shovel.png'; const docStyle = document.body.style; document.querySelector('img').addEventListener('click', () => { if (!docStyle.cursor) docStyle.cursor = `url('${src}'), default`; else docStyle.cursor = null; });` – user9192911 May 02 '18 at 09:46
  • `doesn't work` is vague and impossible to debug - the code is fine in the snippet and in the fiddle, as you can see – CertainPerformance May 02 '18 at 09:48
  • To be more precise, I'm calling a local image so I was wondering if I'm referring to it incorrectly in my code hence why the code doesn't work or it doesn't make any difference? `const src = 'images/Shovel.png'` – user9192911 May 02 '18 at 09:56
  • That's the right syntax. `images` should be a child directory of the base directory. You can look at your browser's Network tab to see where the request is going, and compare that to your filesystem structure. You could also see what happens with a `` in your HTML. – CertainPerformance May 02 '18 at 10:01
  • I see the image in my HTML but when I look at the Network tab nothing happens when I click. I updated the code (https://jsfiddle.net/r9Lbmhxv/3/) and replaced the original img with the image I'm using. I can see the image but when I click on it the pointer stays the same. I don't think it has to with format but I still cannot wrap my head around the cause. – user9192911 May 02 '18 at 11:34
  • You might have to apply the style to a particular container and not the entire `body`. For example, enclose the whole page in a `div`, select that `div`, and assign to its `style.cursor` property. Works on my end, at least. – CertainPerformance May 03 '18 at 02:00
  • Got caught and didn't have the chance to follow up. Wanted to thank you for your help and your time. – user9192911 May 14 '18 at 17:55