Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*global $, document*/
- $.fn.hexGridWidget = function (radius, columns, rows, cssClass) {
- 'use strict';
- var createSVG = function (tag) {
- return $(document.createElementNS('http://www.w3.org/2000/svg', tag || 'svg'));
- };
- return $(this).each(function () {
- var element = $(this),
- hexClick = function () {
- var hex = $(this);
- element.trigger($.Event('hexclick', hex.data()));
- },
- height = 72.43,
- svgParent = createSVG('svg').attr('tabindex', 1).appendTo(element).css({
- width: (1.5 * columns + .5) * radius,
- height: (2 * rows + 1) * height
- }),
- column, row, center,
- toPoint = function (dx, dy) {
- return Math.round(dx + center.x) + ',' + Math.round(dy + center.y);
- };
- for (row = 0; row < rows; row++) {
- for (column = 0; column < columns; column++) {
- center = {x:Math.round((1 + 1.5 * column) * radius), y: Math.round(height * (1 + row * 2 + (column % 2)))};
- createSVG('polygon').attr({
- points: [
- toPoint(-1 * radius / 2, -1 * height),
- toPoint(radius / 2, -1 * height),
- toPoint(radius, 0),
- toPoint(radius / 2, height),
- toPoint(-1 * radius / 2, height),
- toPoint(-1 * radius, 0)
- ].join(' '),
- 'class':cssClass,
- tabindex:1
- })
- .appendTo(svgParent).data({center:center, row:row, column:column}).on('click', hexClick).attr({'hex-row': row, 'hex-column': column});
- }
- }
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement