![]() But there are some technical particularities and restrictions that makes it not so easy. And you may expect some simple and free WordPress plugin could let you do what you want. Here’s what’s under the hood of this tutorial and more details why I created it.Īdding interactivity to images like highlighted areas seems like a basic thing. Some practical tips on using SVG images in your WordPress siteīefore we get down to the tutorial (under the hood).This simple CSS code will make the width of your SVG image correct.SVG Support plugin helps to import and use your SVG images.Adding interactive SVG with raster image inside to your WordPress.Setting the initial color and transparency of the area.Adding an area to the image and edit area’s shape. ![]() ![]() Before we start adding interactive highlighted areas.Adding interactive highlighted areas to your SVG image using Incscape.Take your image and put it inside a new SVG.Tutorial starts: Create SVG from your image or from scratch.Video walk-through of the whole process.Before we get down to the tutorial (under the hood).This is pretty basic, but it's good starting point for creating a fully featured map. The color effect is done with CSS to demonstrate that you can style an SVG element (and its children) with plain CSS. As for the interaction, if you hover a region, you get a popup and the region color changes. OK let's make this thing a bit more useful, I found Belgian population data (2019) on Wikipedia and we're going to plot it on our map, plus we'll make it interactive!īetter? Now we have real data and each region's color represent its population, the colors are shades between green and white, whereas the most populated region (Antwerp province) has a mix of 100% of green and 0% of white, while the least populated (Luxembourg province) has more white than green: this is called a heatmap. Please note that I'm selecting the map regions with draw.find('path') but if you use another map, the regions may be represented by elements or may be inside nested into other elements. In the codepen above we load the map SVG file content with the fetch() method and we render it into the HTML, then we manipulate the map adding labels and changing the colors just for demo purpose. Yeah so easy! I only used a tiny library for SVG manipulation called Svg.js, even if it's not mandatory, it makes life a bit easier. So let's start with step 1 and part of 2: Attach mouse events listeners (for click and hover) to regions.Color the map regions based on supplied data.Load the SVG map and render it into the HTML page.Now we're ready to go, these are the features we're going to implement at the end of the article: When I speak of regions I refer to geographical regions, that is any of country, region, province, state, area, etc. The first step is to find a map in SVG format, I typically use simplemaps they have world and regional maps and they're free to use (check the license for details). Of course there are libraries for this purpose, also free ones, but not so many and they're pretty heavy as they bring a lot of functionalities, whereas I just need a simple map, so I like to make my own map, also because it's fun □. You know what I'm talking about right? Not a Google Maps map, but a simple and plain map with country borders. Now and then, on several web applications projects, I need to use maps to display data, sometimes it's a world map, sometimes a country map with regions and provinces, often I need a click-on-region event handler and I always need to display some data on the map as colors or numbers.
0 Comments
Leave a Reply. |