I have image data of cursor in in 2d byte array .I want to make custom cursor by this raw data.If anybody know please tell how to make by url() function of javascript?
3 Answers
You might be able to use a data
URI to encapsulate the raw pixels, but even then you need to have some kind of format. Are you using a canvas
to create the image? If so, you might be able to use JavaScript to convert it like this:
var canvas = ... // look up canvas node
var cursorAsData = canvas.toDataURL();
// NOTE: jQuery or another library would make this much easier.
var nodeThatNeedsCursor = ... // look up node for cursor
nodeThatNeedsCursor.style.cursor = "url("+cursorAsData+")";
What this does is return the canvas as a data URI, which looks like this:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACt...
This can be used in just about all modern browsers directly in a url()
in a stylesheet.
If you just want to encode the item in a stylesheet, and you have some sort of source image, you can use a Data URI encoder like this one, which lets you upload an image and returns encoded string.
But if you just have a raw byte string for the image, please see this stackoverflow answer for the conversion, then use the data URI scheme above.

- 1
- 1

- 39,252
- 15
- 98
- 100
-
I was wondering if base64 encoding the data would work, but I couldn't think of how you would convert arbitrary data to the necessary format offhand, though. I suspected canvas might be involved. – Stefan Kendall Aug 16 '11 at 04:41
-
Are you referring to Java as in Sun/Oracle? Because you only mentioned JavaScript in your original question. – OverZealous Aug 17 '11 at 12:55
-
nodeThatNeedsCursor.style.cursor = "url("+cursorAsData+")"; it is not working for firefox – user894554 Aug 29 '11 at 11:38
If you want to load a custom cursor (either from a file or from a data url) you also need to include a fallback cursor from the standard set or the browser will ignore the cursor property. (took me an hour of trying to get it to work before finding this little issue) Like this:
item {
cursor: url(image.png), crosshair;
}

- 2,229
- 1
- 19
- 28
create the gif or png using an image editing software, and use css to assign it to the cursor
item {
cursor: url(image.png);
}

- 42,752
- 13
- 76
- 103