Skip to content

Instantly share code, notes, and snippets.

@nswamy14
Last active September 6, 2018 00:56
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nswamy14/df13d67b6efeb19eb640 to your computer and use it in GitHub Desktop.
Save nswamy14/df13d67b6efeb19eb640 to your computer and use it in GitHub Desktop.
ImageMap

Image Map

Simple App with simple logic ,to map image as SVG circles.

You can upload any pic!!!

It has two modes: Play Mode and Slider Mode.

In play Mode: Initially single circle will be rendered, as we hover on a circle, it will be split into four equal circles. As the circle Radius reduces, more refine image appears.

Incase of Slider mode: Slider at the bottom of the circle will be used to perform the split.

Start playing!!!

/****************************
/*
/* Developer: Narayana Swamy
/* emailID: narayanaswamy14@gmail.com
/*
/*****************************/
d3 = d3 || {};
(function() {
"use strict";
d3.imageMap = function() {
var height, width, onDragCallback;
var container;
var canvas, canvasAccessObj, pixelData, circle;
var context;
var diameter, radius;
var slidingBar = d3.buildSlider();
var levels, size;
var scale = d3.scale.linear();
var currentLvl, imageName, mode, selectionLength;
var splitFlag = true;
var obj = function(selection) {
slidingBar.width(diameter)
.height(15)
.partitions(levels)
.onDragCallback(obj.onDragCallback);
selection.each(function() {
container = d3.select(this);
container
.attr('width', diameter)
.attr('height', diameter + 200);
var cg = container.append('g')
.attr('class', 'cg');
if (mode == 'slider')
var sg = container.append('g')
.attr('class', 'sg')
.attr('transform', 'translate(' + 0 + ',' + diameter + ')')
.call(slidingBar);
obj.renderCircle(diameter / 2, diameter / 2, diameter / 2, 0, 0);
scale = scale.range([diameter / 2, diameter]).domain([currentLvl - 1, currentLvl]);
})
};
obj.buildCanvas = function() {
var img = document.getElementById('myimage');
var unit = 0,
width = 0;
height = 0;
if(size){
width = size[0];
height = size[1];
}
else{
width = img.naturalWidth;
height = img.naturalHeight;
}
img.height = height;
img.width = width;
diameter = d3.max([width, height]);
canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
context = canvas.getContext('2d')
context.drawImage(img, 0, 0, width, height);
canvasAccessObj = canvas.getContext('2d');
levels = Math.floor(Math.log2(diameter));
currentLvl = levels;
return obj;
};
obj.onDragCallback = function(_) {
if (diameter / 2 <= 2) return;
if (parseInt(scale(_)) < parseInt(diameter / 2)) {
if (!slidingBar.startStop()) return;
slidingBar.startStop(false);
selectionLength = container
.select('.cg')
.selectAll('circle')
.size();
container
.select('.cg')
.selectAll('circle')
.each(obj.breakCircle);
diameter = diameter / 2;
currentLvl = currentLvl - 1;
scale.range([diameter / 2, diameter]).domain([currentLvl - 1, currentLvl]);
} else {
if (diameter > 20)
obj.renderCircleGroup(scale(_) / 2);
}
return true;
};
obj.renderCircleGroup = function(r) {
container
.selectAll('circle')
.attr('r', r);
};
obj.breakCircle = function(d, i) {
var c = d3.select(this);
var t_x = parseFloat(c.attr('cx'));
var t_y = parseFloat(c.attr('cy'));
var t_r = mode == 'slider' ? diameter / 2 : (c.attr('r'));
if (t_r < 2){
splitFlag = true;
return
};
var r_ = (t_r / 2);
var cx, cy, c1x, c1y, c2x, c2y, c3x, c3y, xF, yF;
if (mode == 'slider') {
//SliderMode
c.attr('r', r_)
.attr('cx', t_x + (r_))
.attr('cy', t_y - (r_))
.each(function() {
setTimeout(obj.renderCircle(t_x - (r_), t_y - (r_), r_, t_x, t_y, t_r), 0);
setTimeout(obj.renderCircle(t_x - (r_), t_y + (r_), r_, t_x, t_y, t_r), 0);
setTimeout(obj.renderCircle(t_x + (r_), t_y + (r_), r_, t_x, t_y, t_r), 0);
if (i === selectionLength - 1) {
setTimeout(function() {
slidingBar.startStop(true);
}, 1000);
}
});
} else {
xF = (d3.mouse(this)[0] > t_x) ? 1 : -1;
yF = (d3.mouse(this)[1] > t_y) ? 1 : -1;
cx = c3x = t_x + (xF) * r_;
c1x = c2x = t_x + (xF * -1) * r_;
cy = c1y = t_y + (yF) * r_;
c2y = c3y = t_y + (yF * -1) * r_;
var pixelData = context.getImageData(parseInt(cx), parseInt(cy), 1, 1).data;
c.style('opacity',0.3)
.transition()
.duration(mode === 'play'?r_*5:0)
.attr('r', r_)
.attr('cx', cx)
.attr('cy', cy)
.style('opacity',1)
.style('fill', 'rgb(' + pixelData[0] + ',' + pixelData[1] + ',' + pixelData[2] + ')')
.each(function() {
obj.renderCircle(c1x, c1y, r_, t_x, t_y, t_r);
obj.renderCircle(c2x, c2y, r_, t_x, t_y, t_r);
obj.renderCircle(c3x, c3y, r_, t_x, t_y, t_r);
});
}
};
obj.renderCircle = function(x, y, r, p_x, p_y, t_r) {
var pixelData = context.getImageData(parseInt(x), parseInt(y), 1, 1).data;
circle = container.select('.cg')
.append('circle');
circle.attr('cx', p_x)
.attr('cy', p_y)
.attr('r', t_r)
.style('fill', 'rgb(' + pixelData[0] + ',' + pixelData[1] + ',' + pixelData[2] + ')')
.transition()
.duration(mode === 'play'?r*5:0)
.attr('cx', x)
.attr('cy', y)
.attr('r', r)
.each('end',function(){
splitFlag = true;
})
if (mode === 'play') {
circle.on('mouseover', function() {
if (mode === 'play' && splitFlag){
splitFlag = false;
obj.breakCircle.bind(this)();
}
})
}
return;
};
obj.height = function(_) {
if (!arguments.length) return height;
height = _;
return obj;
};
obj.width = function(_) {
if (!arguments.length) return width;
width = _;
return obj;
};
obj.imageName = function(_) {
if (!arguments.length) return imageName;
imageName = _;
return obj;
};
obj.setMode = function(_) {
if (!arguments.length) return setMode;
mode = _;
return obj;
};
obj.setImageSize = function(_) {
if (!arguments.length) return size;
size = _;
return obj;
};
return obj;
}
})()
<!DOCTYPE html>
<html lang="en">
<head>
<title>ImageMap</title>
<!-- <link href="styles/css/it-mlaf.min.css" rel="stylesheet"> -->
<style type="text/css">
#imgParent{
display: none;
}
#buttons{
position: relative;
text-align: center;
display: block;
}
#chart{
position: relative;
top: 20px;
text-align: center;
}
#msg{
display: block;
margin: 10px;
font-family: sans-serif;
font-size: 15px;
}
</style>
</head>
<body>
<div>
<div id='imgInput'>
Upload Image : <input type="file" onchange="onFileSelected(event)" value="chose picture">
</div>
<div id="buttons">
<button id="slider" onclick="setMode('slider')" style="background-color:rgb(255, 255, 255)">Auto Slider Mode</button>
<button id="play" onclick="setMode('play')" style="background-color:rgb(150, 236, 241)">Play Mode</button>
<p id='msg'> Move mouse pointer In/Out of a circle</p>
</div>
<div id="imgParent">
<img id="myimage">
</div>
<div id="chart"></div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.6/d3.min.js" charset="utf-8"></script>
<script type="text/javascript" src="sliding_bar.js"></script>
<script type="text/javascript" src="imageMap.js"></script>
<script>
var imgtag = document.getElementById("myimage");
//Setting Default Image, you can load any image using input box.
imgtag.src = "uchiha_madara.jpg";
imgtag.onload = render;
var imageMap = d3.imageMap()
.setMode('play')
.setImageSize([512,512]);
function setMode(_){
d3.select(_==='play'?'#slider':'#play').style('background-color','rgb(255, 255, 255)')
d3.select('#'+_).style('background-color','rgb(150, 236, 241)')
if(_ == 'play')
d3.select('#msg').style('display','block');
else
d3.select('#msg').style('display','none');
imageMap.setMode(_);
render();
}
function onFileSelected(event) {
var selectedFile = event.target.files[0];
var reader = new FileReader();
imgtag.title = selectedFile.name;
reader.onload = function(event) {
imgtag.src = event.target.result;
};
reader.readAsDataURL(selectedFile);
}
function render(){
imageMap.buildCanvas();
d3.select('#chart').selectAll('svg').remove();
var svg = d3.select('#chart')
.append('svg')
.call(imageMap);
}
</script>
</body>
</html>
MIT License
Copyright (c) 2017 Narayana Swamy
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
/****************************
/*
/* Developer: Narayana Swamy
/* emailID: narayanaswamy14@gmail.com
/*
/*****************************/
d3 = d3||{};
(function(){
"use strict";
d3.buildSlider = function(){
var height,width,onDragCallback,startStop = true;
var scale = d3.scale.linear();
var msg = "loading...",loadingMsg,partitions;
var obj = function(selection){
var x = width;
scale.range([0,partitions]).domain([0,width]);
var drag = d3.behavior.drag()
.on('dragstart', function () {
})
.on('drag', function (d, i) {
if(!startStop) return;
if(d3.event.dx>0) return;
x += d3.event.dx;
if(x<0 || x>width-10) return;
d3.select(this).attr("x", x);
d3.select('.loading').attr("x", x-30);
return onDragCallback(scale(x));
})
.on('dragend',function(){
});
selection.each(function(d){
var container = d3.select(this);
var parentBar = container.append('rect')
.attr('x',0)
.attr('y',0)
.attr('width',width)
.attr('height',height)
.attr('rx',5)
.attr('ry',5)
.style('fill','#44ABA9');
var slidingBar = container.append('rect')
.attr('class','slidingBar')
.attr('x',width-10)
.attr('y',-2.5)
.attr('rx',3)
.attr('ry',3)
.attr('width',10)
.attr('height',20)
.style('fill','#00E1FF')
.call(drag);
loadingMsg = container.append('text')
.attr('x',width-30)
.attr('y',30)
.attr('class','loading')
.style('font-size','15px')
.style('fill','black')
.style('display','none')
.text(msg);
})
}
obj.height = function(_){
if(!arguments.length) return height;
height = _;
return obj;
};
obj.width = function(_){
if(!arguments.length) return width;
width = _;
return obj;
};
obj.onDragCallback = function(_){
if(typeof _ !== 'function') return;
onDragCallback = _;
return obj;
};
obj.partitions = function(_){
if(!arguments.length) return partitions;
partitions = _;
return obj;
};
obj.startStop = function(_){
if(!arguments.length) return startStop;
startStop = _;
if(!_)
loadingMsg.style('display','block');
else
loadingMsg.style('display','none')
return obj;
};
return obj;
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment