Why can't I draw elements to the stage?

Michael Schwartz

Fiddle - http://liveweave.com/JS9EBN

This is a starter template for web design applications. (well almost except for the drawing problem)

Elements are drawn to the stage like so.

// Handles Drawable Elements
$("#canvas").on('mousedown touchstart', function(e) {
  if(drawable) {
    drawing = true;
    mS.x = e.pageX;
    mS.y = e.pageY;
    dBox = $("<" + $('.draw-elements input[type=radio]:checked').val() + " class='box' />")
            .html("Certains textes");
    $(this).append(dBox);
    
    // Do not select text when drawing
    return false;
  }
});
$(document).on('mousemove touchmove', function(e) {
  if(drawing && drawable){
    var mPos = {x:e.pageX, y:e.pageY};
    var css = {};
      css.position   = 'absolute';
      css.left       = (mPos.x > mS.x) ? mS.x : mPos.x;
      css.top        = (mPos.y > mS.y) ? mS.y : mPos.y;
      css.width      = Math.abs(mPos.x - mS.x);
      css.height     = Math.abs(mPos.y - mS.y);
      css.border = '1px dotted rgb(0, 34, 102)';
      dBox.css(css);
    
    // Do not select text when drawing
    return false;
  }
}).on('mouseup touchend', function(e) {
  drawing  = false;
});

As long as my select tool is not called I can draw elements without a problem, but when it is called and I come back to draw a div I can no longer draw elements to my stage.

After some tinkering I noticed the problem resides with my drag function for each element that's selected.

var HandleSelectedElement = function() {
  if ($(".select-tool.activetool").is(":visible")) {
    if(elmstyle) {
      $('#canvas').children().drag("start",function( ev, dd ){
      dd.attrc = $( ev.target ).prop("className");
      dd.attrd = $( ev.target ).prop("id");
      dd.width = $( this ).width();
      dd.height = $( this ).height();
    })
    .drag(function( ev, dd ){
      var props = {};
      if ( dd.attrc.indexOf("E") > -1 ){
        props.width = Math.max( 32, dd.width + dd.deltaX );
      }
      if ( dd.attrc.indexOf("S") > -1 ){
        props.height = Math.max( 32, dd.height + dd.deltaY );
      }
      if ( dd.attrc.indexOf("W") > -1 ){
        props.width = Math.max( 32, dd.width - dd.deltaX );
        props.left = dd.originalX + dd.width - props.width;
      }
      if ( dd.attrc.indexOf("N") > -1 ){
        props.height = Math.max( 32, dd.height - dd.deltaY );
        props.top = dd.originalY + dd.height - props.height;
      }
      if ( dd.attrd.indexOf("stylethis") > -1 ){
        props.top = dd.offsetY;
        props.left = dd.offsetX;
      }
      $('#stylethis').css( props );
    }, {relative:true});
  }
};

I don't understand what's wrong.

Michael Schwartz

I couldn't find out how to solve the initial problem. So I decided to do a work around.

Here's the fiddle: http://liveweave.com/g2aKlD

I decided to refresh the canvas each time a tool is clicked. I take the canvas's html and set that as a textarea's value. I then set the textarea's value as the canvas's html.

$("#code").val($("#canvas").html());
$("#canvas").html($("#code").val());

This way whatever called the initial bug is removed completely. However last time I did this all spaces where turned into a &nbsp; otherwise known as a non-breakable space (I've also seen this also add unnecessary line breaks and paragraphs when not needed)

I still don't understand why my draw function did not work when drag was turned off.

I really hate having to do this (being refreshing the canvas) because the more elements that are in there and the more refreshing is done, the more ram it uses which makes the browser work the cpu that much harder.

I hope someone can come across a better solution.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why can't I draw an ellipse with this code?

From Dev

Why can't I draw a texture in pyOpenGL

From Dev

why I can't accessing this array elements

From Dev

Why can't I draw on my image's canvas?

From Dev

I can't get a transparent stage in JavaFX

From Dev

I can't get a transparent stage in JavaFX

From Dev

Why can't I query directly on jsonb_array_elements?

From Dev

Why can't I reassign elements to an array that was deallocated with delete []?

From Dev

Why can't I push_back to a vector of const elements?

From Dev

Why can't I identify the elements on the history page in Chrome?

From Dev

Why can't I access the elements of my NSMutableArray?

From Dev

Why can't I specify NamedArgs and item elements on cellFactory?

From Dev

Why can't I position the elements correctly on the circumference of a circle?

From Dev

Why can't I draw a line on this D3.js using angular code?

From Dev

Why Can't I override draw method from cocos2d::sprite?

From Dev

Why can't I draw a dashed line with different line caps/joins on OS X using Quartz?

From Dev

Why can't I draw a complete circle with arc() in Three.js?

From Dev

I can't draw a circle in swing

From Dev

I can't draw a filled rectangle

From Dev

Why can't the deploy stage of my Bluemix devops pipeline find the WAR file that was created in the build stage?

From Dev

libgdx stage doesn't draw stretched image

From Dev

JCanvas didn't work I can't draw anything

From Dev

Why can't .each() find the child elements?

From Dev

Why can't inline elements be transformed?

From Dev

Why can't I change the number of elements / buses in the input scope of AU multi channel mixer?

From Dev

Why do I get an error of can't make a table of more 2^31 elements in R

From Dev

Why can't I override existing :after and :before pseudo-elements?

From Dev

Why can't I get two <p> elements to be side-by-side and aligned by center?

From Dev

Why I can't to this assignment?

Related Related

  1. 1

    Why can't I draw an ellipse with this code?

  2. 2

    Why can't I draw a texture in pyOpenGL

  3. 3

    why I can't accessing this array elements

  4. 4

    Why can't I draw on my image's canvas?

  5. 5

    I can't get a transparent stage in JavaFX

  6. 6

    I can't get a transparent stage in JavaFX

  7. 7

    Why can't I query directly on jsonb_array_elements?

  8. 8

    Why can't I reassign elements to an array that was deallocated with delete []?

  9. 9

    Why can't I push_back to a vector of const elements?

  10. 10

    Why can't I identify the elements on the history page in Chrome?

  11. 11

    Why can't I access the elements of my NSMutableArray?

  12. 12

    Why can't I specify NamedArgs and item elements on cellFactory?

  13. 13

    Why can't I position the elements correctly on the circumference of a circle?

  14. 14

    Why can't I draw a line on this D3.js using angular code?

  15. 15

    Why Can't I override draw method from cocos2d::sprite?

  16. 16

    Why can't I draw a dashed line with different line caps/joins on OS X using Quartz?

  17. 17

    Why can't I draw a complete circle with arc() in Three.js?

  18. 18

    I can't draw a circle in swing

  19. 19

    I can't draw a filled rectangle

  20. 20

    Why can't the deploy stage of my Bluemix devops pipeline find the WAR file that was created in the build stage?

  21. 21

    libgdx stage doesn't draw stretched image

  22. 22

    JCanvas didn't work I can't draw anything

  23. 23

    Why can't .each() find the child elements?

  24. 24

    Why can't inline elements be transformed?

  25. 25

    Why can't I change the number of elements / buses in the input scope of AU multi channel mixer?

  26. 26

    Why do I get an error of can't make a table of more 2^31 elements in R

  27. 27

    Why can't I override existing :after and :before pseudo-elements?

  28. 28

    Why can't I get two <p> elements to be side-by-side and aligned by center?

  29. 29

    Why I can't to this assignment?

HotTag

Archive