I have a problem to solve. In my canvas image I put a marker but this is image type "png", I would like to have an icon svg so I can change the colour.
This code is working, i need to change .png with .svg, but doesn’t work.
var Marker = function () {
this.Sprite = new Image();
this.Sprite.src="http://www.clker.com/cliparts/w/O/e/P/x/i/map-marker-hi.png";
this.Width = 20;
this.Height = 32;
this.XPos = 0;
this.YPos = 0;
this.textarea=null;
}
2
Answers
Replace the
this.Sprite.src
with an SVG image source.Add a CSS class to the SVG element to apply styling.
Use CSS to change the color of the SVG icon.
Here’s the modified code:
In your HTML or CSS, define the
marker-icon
class and specify the color you want:Replace
"your-icon.svg"
with the path to your SVG icon file, and set thefill
property in the CSS class to the color you want for your marker. This way, you can change the color by simply modifying thefill
property in your CSS, and you have the flexibility to use SVG icons with different colors.I hope that helps
Calling
ctx.drawImage
on load should do the trick. Drawing SVG files in this manner is supported on all major modern browsers except for IEWorking jsfiddle
MDN
drawImage
docs