English 中文(简体)
d3.js 根据改变用途运动更新线路
原标题:d3.js update line based on mouse movement

我只想挖 into3,学习很多东西,但我相信我会在这里忽略/误解一些基本内容,并会爱一些指导。

Basically, I d like to utilize d3 to draw a line segment that follows the mouse from the mousedown event (x1, y1) update the (x2, y2) coordinates as the mousemove event fires and finally finish/stop drawing the line once mouseup fires (finalizing the (x2, y2) coordinates).

审视这一jsFiddle I ve ,以说明I m试图做些什么:

Obviously the issue is that the line isn t updating as the mouse moves. It s likely I m going about this completely wrong, so any sort of guidance would be appreciated. Thanks!

UPDATE I see there is d3.behavior.drag extensions, but I m not sure if that s exactly the type of thing I d need or not (since the user isn t exactly interacting with any objects, rather creating a new one on the fly (line object))

UPDATE 2 Okay, I m trying to go about it a bit differently here, and it s almost there I think. Although I m sure there s a much better way to do this. What s below is close (but it doesn t draw the lines).

    var shapeCoords = [
              [10, 10], [200, 10], [200, 200], [10, 200], [5, 100]
              ];

$(function() {
var container = $( #container );

// D3
console.log("D3: ", d3);

// Draw Shape
var svg = d3.select( #container ).append( svg:svg ).attr( height , 600).attr( width , 800);


var line = d3.svg.line()
                .x(function(d) { return d[0]; })
                .y(function(d) { return d[1]; })
                .interpolate( linear );

svg
    .data(shapeCoords)
    .append( svg:path )
    .attr( d , line(shapeCoords) +  Z )
    .style( stroke-width , 1)
    .style( stroke ,  steelblue )
    .style( fill ,  rgba(120, 220, 54, 0.2) );



// Draw Lines

var lineData = [];

var line = d3.svg.line()
                .x(function(d) { return d.x; })
                .y(function(d) { return d.y; })
                .interpolate("linear");

var redrawLine = function() {
    var svgLines = svg.selectAll( path )
                        .data(lineData);


    svgLines.enter()
            .append( path )
            .attr( d , line(lineData))
            .attr( class ,  my-lines );

    svgLines.exit()
            .remove();

    console.debug("lineData", lineData);
};

var mouseIsDown = false;  
container.on( mousedown mouseup mousemove , function(e) {
    if (e.type ==  mousedown ) {
        mouseIsDown = true; 
        lineData[0] = {x:e.offsetX, y:e.offsetY};
        redrawLine();
    } else if (e.type ==  mouseup  ){
        mouseIsDown = false;
        lineData[1] = {x:e.offsetX, y:e.offsetY};
        redrawLine();
    } else if (e.type ==  mousemove ) {
        if (mouseIsDown) {
            lineData[1] = {x:e.offsetX, y:e.offsetY};
            redrawLine();    
        }
    }
});


});

​I ve updated the jsFiddle here: http://jsfiddle.net/555Cv/1/

最佳回答

Ok, 我认为我已经解决,见jsFiddle:。 http://jsfiddle.net/555Cv/2/

但是,这样做肯定必须更加合法/适当。

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签