javascript - transforming mouse coordinates in CANVAS using fabric.js -
i have designed ruler using fabric.js , when user mouses on specific part of ruler want print screen x-coordinate of ruler (i.e. not screen coordinate). right ruler canvas starts @ position 37 , ends @ 726 relative ruler, ruler goes 1 4600 (it start @ 1 length of ruler can change). how transform mouse coordinates accurately reflect it's position on ruler? here code:
var canvas = new fabric.canvas('canvas'); line_length = input_len; adjusted_length = (line_length / 666) * 100; canvas.add(new fabric.line([0, 0, adjusted_length, 0], { left: 30, top: 0, stroke: '#d89300', strokewidth: 3 })); $('#canvas_container').css('overflow-x', 'scroll'); $('#canvas_container').css('overflow-y', 'hidden'); drawruler(); function drawruler() { $("#ruler[data-items]").val(line_length / 200); $("#ruler[data-items]").each(function () { var ruler = $(this).empty(), len = number($("#ruler[data-items]").val()) || 0, item = $(document.createelement("li")), i; ruler.append(item.clone().text(1)); (i = 1; < len; i++) { ruler.append(item.clone().text(i * 200)); } ruler.append(item.clone().text(i * 200)); }); } canvas.add(new fabric.text('x-cord', { fontstyle: 'italic', fontfamily: 'hoefler text', fontsize: 12, left: 0, top: 0, hascontrols: false, selectable: false })); canvas.on('mouse:move', function (options) { getmouse(options); }); function getmouse(options) { canvas.getobjects('text')[0].text = "x-coords: " + options.e.clientx ; //+ " y: " + options.e.clienty; canvas.renderall(); }
use getpointer merthod on canvas instance. in case should canvas.getpointer(options.e)
which returns object x , y properties represent pointer coordinates relative canvas.
Comments
Post a Comment