Skip to content
This repository has been archived by the owner on Jun 14, 2020. It is now read-only.

Commit

Permalink
SVG plugin now supports complex shapes, just like Imagemap plugin, vi…
Browse files Browse the repository at this point in the history
…a use of generalized polys plugin used by both.
  • Loading branch information
Craga89 committed Apr 3, 2013
1 parent 32391d4 commit 55238d1
Show file tree
Hide file tree
Showing 7 changed files with 243 additions and 222 deletions.
6 changes: 3 additions & 3 deletions Gruntfile.js
Expand Up @@ -44,12 +44,12 @@ module.exports = function(grunt) {
css3: '<%=dirs.src%>/css3.css'
},
plugins: {
svg: { js: '<%=dirs.src%>/svg/svg.js' },
ajax: { js: '<%=dirs.src%>/ajax/ajax.js' },
tips: { js: '<%=dirs.src%>/tips/tips.js', css: '<%=dirs.src%>/tips/tips.css' },
modal: { js: '<%=dirs.src%>/modal/modal.js', css: '<%=dirs.src%>/modal/modal.css' },
viewport: { js: '<%=dirs.src%>/viewport/viewport.js' },
imagemap: { js: '<%=dirs.src%>/imagemap/imagemap.js' },
viewport: { js: '<%=dirs.src%>/position/viewport.js' },
svg: { js: [ '<%=dirs.src%>/position/polys.js', '<%=dirs.src%>/position/svg.js' ] },
imagemap: { js: [ '<%=dirs.src%>/position/polys.js', '<%=dirs.src%>/position/imagemap.js' ] },
ie6: { js: '<%=dirs.src%>/ie6/ie6.js', css: '<%=dirs.src%>/ie6/ie6.css' }
},

Expand Down
172 changes: 0 additions & 172 deletions src/imagemap/imagemap.js

This file was deleted.

43 changes: 43 additions & 0 deletions src/position/imagemap.js
@@ -0,0 +1,43 @@
PLUGINS.imagemap = function(api, area, corner, adjustMethod)
{
if(!area.jquery) { area = $(area); }

var shape = area.attr('shape').toLowerCase().replace('poly', 'polygon'),
image = $('img[usemap="#'+area.parent('map').attr('name')+'"]'),
coordsString = area.attr('coords'),
coordsArray = coordsString.split(','),
imageOffset, coords, i, next;

// If we can't find the image using the map...
if(!image.length) { return FALSE; }

// Pass coordinates string if polygon
if(shape === 'polygon') {
result = PLUGINS.polys.polygon(coordsArray, corner);
}

// Otherwise parse the coordinates and pass them as arguments
else if(PLUGINS.polys[shape]) {
for(i = -1, len = coordsArray.length, coords = []; ++i < len;) {
coords.push( parseInt(coordsArray[i], 10) );
}

result = PLUGINS.polys[shape].apply(
this, coords.concat(corner)
);
}

// If no shapre calculation method was found, return false
else { return FALSE; }

// Make sure we account for padding and borders on the image
imageOffset = image.offset();
imageOffset.left += Math.ceil((image.outerWidth() - image.width()) / 2);
imageOffset.top += Math.ceil((image.outerHeight() - image.height()) / 2);

// Add image position to offset coordinates
result.position.left += imageOffset.left;
result.position.top += imageOffset.top;

return result;
};
115 changes: 115 additions & 0 deletions src/position/polys.js
@@ -0,0 +1,115 @@
PLUGINS.polys = {
// POLY area coordinate calculator
// Special thanks to Ed Cradock for helping out with this.
// Uses a binary search algorithm to find suitable coordinates.
polygon: function(baseCoords, corner) {
var result = {
width: 0, height: 0,
position: {
top: 1e10, right: 0,
bottom: 0, left: 1e10
},
adjustable: FALSE
},
i = 0, next,
coords = [],
compareX = 1, compareY = 1,
realX = 0, realY = 0,
newWidth, newHeight;

// First pass, sanitize coords and determine outer edges
i = baseCoords.length; while(i--) {
next = [ parseInt(baseCoords[--i], 10), parseInt(baseCoords[i+1], 10) ];

if(next[0] > result.position.right){ result.position.right = next[0]; }
if(next[0] < result.position.left){ result.position.left = next[0]; }
if(next[1] > result.position.bottom){ result.position.bottom = next[1]; }
if(next[1] < result.position.top){ result.position.top = next[1]; }

coords.push(next);
}

// Calculate height and width from outer edges
newWidth = result.width = Math.abs(result.position.right - result.position.left);
newHeight = result.height = Math.abs(result.position.bottom - result.position.top);

// If it's the center corner...
if(corner.abbrev() === 'c') {
result.position = {
left: result.position.left + (result.width / 2),
top: result.position.top + (result.height / 2)
};
}
else {
// Second pass, use a binary search algorithm to locate most suitable coordinate
while(newWidth > 0 && newHeight > 0 && compareX > 0 && compareY > 0)
{
newWidth = Math.floor(newWidth / 2);
newHeight = Math.floor(newHeight / 2);

if(corner.x === LEFT){ compareX = newWidth; }
else if(corner.x === RIGHT){ compareX = result.width - newWidth; }
else{ compareX += Math.floor(newWidth / 2); }

if(corner.y === TOP){ compareY = newHeight; }
else if(corner.y === BOTTOM){ compareY = result.height - newHeight; }
else{ compareY += Math.floor(newHeight / 2); }

i = coords.length; while(i--)
{
if(coords.length < 2){ break; }

realX = coords[i][0] - result.position.left;
realY = coords[i][1] - result.position.top;

if((corner.x === LEFT && realX >= compareX) ||
(corner.x === RIGHT && realX <= compareX) ||
(corner.x === CENTER && (realX < compareX || realX > (result.width - compareX))) ||
(corner.y === TOP && realY >= compareY) ||
(corner.y === BOTTOM && realY <= compareY) ||
(corner.y === CENTER && (realY < compareY || realY > (result.height - compareY)))) {
coords.splice(i, 1);
}
}
}
result.position = { left: coords[0][0], top: coords[0][1] };
}

return result;
},

rect: function(ax, ay, bx, by, corner) {
return {
width: Math.abs(bx - ax),
height: Math.abs(by - ay),
position: {
left: Math.min(ax, bx),
top: Math.min(ay, by)
}
};
},

_angles: {
tc: 3 / 2, tr: 7 / 4, tl: 5 / 4,
bc: 1 / 2, br: 1 / 4, bl: 3 / 4,
rc: 2, lc: 1, c: 0
},
ellipse: function(cx, cy, rx, ry, corner) {
var c = PLUGINS.polys._angles[ corner.abbrev() ],
rxc = rx * Math.cos( c * Math.PI ),
rys = ry * Math.sin( c * Math.PI );

return {
width: (rx * 2) - Math.abs(rxc),
height: (ry * 2) - Math.abs(rys),
position: {
left: cx + rxc,
top: cy + rys
},
adjustable: FALSE
};
},
circle: function(cx, cy, r, corner) {
return PLUGINS.polys.ellipse(cx, cy, r, r, corner);
}
};

0 comments on commit 55238d1

Please sign in to comment.