Skip to content

Instantly share code, notes, and snippets.

@joyrexus
Last active December 24, 2015 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joyrexus/6804727 to your computer and use it in GitHub Desktop.
Save joyrexus/6804727 to your computer and use it in GitHub Desktop.
Cursor extension for svg.js
<!DOCTYPE html>
<meta charset="utf-8">
<script src="https://s3-eu-west-1.amazonaws.com/svgjs/svg.js"></script>
<script src="http://jashkenas.github.io/coffee-script/extras/coffee-script.js"></script>
<script src="svg.cursor.js"></script>
<style>
rect {
fill: none;
stroke: #999;
stroke-width: 2;
}
line {
stroke-width: 2;
opacity: 0.5;
}
</style>
<body>
<div id="canvas"></div>
<script type="text/coffeescript">
w = 960
h = 500
draw = SVG('canvas')
draw.rect(w, h)
d = 400 # diameter
r = d / 2 # radius
c =
x: w / 2
y: h / 2
style =
fill: "aliceblue"
stroke: "steelblue"
'stroke-width': 10
draw.circle(d+2) # diameter + stroke width of markers
.center(c.x, c.y)
.attr(style)
draw.cursor(c.x, c.y)
.lineTo(c.x, c.y+r)
.lineTo(c.x+r, c.y, "orange")
.markPoint()
.moveTo(c.x, c.y-r)
.markPoint()
.lineTo(c.x-r, c.y, "orange")
.lineTo(c.x, c.y)
</script>
class Cursor
markerStyle:
fill: "#999"
stroke: "aliceblue"
'stroke-width': 2
constructor: (@x=0, @y=0, @draw) ->
lineTo: (x, y, color="steelblue") ->
@draw.line(@x,@y, x,y).attr({stroke: color})
@x = x
@y = y
@
moveTo: (x, y) -> @lineTo(x, y, "none")
markPoint: (size=10, style) ->
style or= @markerStyle
@draw.circle(size).attr(style).center(@x, @y)
@
extension =
cursor: (x, y) -> new Cursor x, y, @
SVG.extend(SVG.Doc, extension)
// Generated by CoffeeScript 1.6.3
(function() {
var Cursor, extension;
Cursor = (function() {
Cursor.prototype.markerStyle = {
fill: "#999",
stroke: "aliceblue",
'stroke-width': 2
};
function Cursor(x, y, draw) {
this.x = x != null ? x : 0;
this.y = y != null ? y : 0;
this.draw = draw;
}
Cursor.prototype.lineTo = function(x, y, color) {
if (color == null) {
color = "steelblue";
}
this.draw.line(this.x, this.y, x, y).attr({
stroke: color
});
this.x = x;
this.y = y;
return this;
};
Cursor.prototype.moveTo = function(x, y) {
return this.lineTo(x, y, "none");
};
Cursor.prototype.markPoint = function(size, style) {
if (size == null) {
size = 10;
}
style || (style = this.markerStyle);
this.draw.circle(size).attr(style).center(this.x, this.y);
return this;
};
return Cursor;
})();
extension = {
cursor: function(x, y) {
return new Cursor(x, y, this);
}
};
SVG.extend(SVG.Doc, extension);
}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment