为jQuery dRawr工具箱添加id属性

huangapple go评论62阅读模式
英文:

Adding id attribute to jQuery dRawr toolbox

问题

我正在使用 jQuery Drawr Canvas,需要在同一页上使用多个画布部分。我已经成功实现了所有功能,除了画笔工具栏不会停留在其相应的画布旁边。如果只有一个元素(画布),我可以设置偏移量,但是如果有多个元素,我无法设置偏移量,因为生成的元素只有一个类别,被视为一个元素。

我尝试通过添加ID属性来解决此问题。但是只对最后一个元素有效。在我的示例中,下面的所有工具栏都具有数组中的最后一个元素作为其ID。

示例(请注意,此数组只是数字 24、25、26):

var question_secs = <?php echo json_encode($question_section_arr); ?>;
var arrayLength = question_secs.length;
$(document).ready(function(){
    for (var i = 0; i < arrayLength; i++) {
        var section = question_secs[i];
        // 将画布元素转化为绘图区域
        $("#drawr-container-"+ section + " .demo-canvas-"+ section).drawr({
            "enable_transparency" : false
        });

        // 启用绘图模式,显示控件
        $("#drawr-container-"+ section + " .demo-canvas-"+ section).drawr("start");

        var offset = $("#canvas-"+ section).offset();
        var topoff = offset.top + 1127;
        var leftoff = offset.left - 83;

        $('.drawr-toolbox').attr('id', 'toolbox-' + section);

        $('#toolbox-' + section).css({top: topoff, left: leftoff, position:'absolute'});
    }
});

在我的示例中,我得到了三个正常工作的画布和画笔工具栏,但工具栏位于页面上的完全错误位置。这个元素 "drawr-toolbox" 是由库生成的,它假设您在页面上只有一个,因此没有ID,因此我无法设置不同的偏移量。我想我可以修改库文件,但这对我来说似乎是不好的做法。

注意:这是从库中创建工具箱元素的函数。

英文:

I'm using jquery drawr canvas and I need multiple canvas sections on one page. I've got it working all correctly, except the toolbar of brushes do not stay on the page next to their respective canvases. I can set the offset if it's just one element(canvas) but with multiple I cannot, as the element it generates only has a class and seen as one element.

I'm trying to solve this by adding an ID attribute. It works but only for the last element. All the tool bars in my example below have the ID that is the last element in the array.

Example(note this array is just numbers 24,25,26):

var question_secs = &lt;?php echo json_encode($question_section_arr); ?&gt;;
var arrayLength = question_secs.length;
$(document).ready(function(){
	for (var i = 0; i &lt; arrayLength; i++) {
		var section = question_secs[i];
   //Turn a canvas element into a sketch area
   $(&quot;#drawr-container-&quot;+ section +&quot; .demo-canvas-&quot;+ section +&quot;&quot;).drawr({
		&quot;enable_transparency&quot; : false
	});

	//Enable drawing mode, show controls
	$(&quot;#drawr-container-&quot;+ section +&quot; .demo-canvas-&quot;+ section +&quot;&quot;).drawr(&quot;start&quot;);

	var offset = $(&quot;#canvas-&quot;+ section +&quot;&quot;).offset();
	var topoff = offset.top + 1127;
	var leftoff = offset.left - 83;

    $(&#39;.drawr-toolbox&#39;).attr(&#39;id&#39;, &#39;toolbox-&#39; + section);

    $(&#39;#toolbox-&#39; + section).css({top: topoff, left: leftoff, position:&#39;absolute&#39;});
	
	
}

});

In my example here I get three working canvas and brush toolboxes but the toolboxes are in completely the wrong place on the page? this element "drawr-toolbox" is generated by the library and it assumes you are only have one on the page so no id, and therefore I cannot set separate offsets? I suppose I could go changing the library file but that seems like bad practice to me.

Note: this is the function that is creating the toolbox element from the library

   /* Create floating dialog and appends it hidden after the canvas */
    plugin.create_toolbox = function(id,position,title,width){
    	var self = this;
		var toolbox = document.createElement(&quot;div&quot;);
		toolbox.innerHTML=&quot;&lt;div style=&#39;padding:5px 0px 5px 0px&#39;&gt;&quot; + title + &quot;&lt;/div&gt;&quot;;
		toolbox.className = &quot;drawr-toolbox drawr-toolbox-&quot; + id;
		toolbox.ownerCanvas = self;
		$(toolbox).css({
			&quot;position&quot; : &quot;absolute&quot;, &quot;z-index&quot; : 6, &quot;cursor&quot; : &quot;move&quot;, &quot;width&quot; : width + &quot;px&quot;, &quot;height&quot; : &quot;auto&quot;, &quot;color&quot; : &quot;#fff&quot;,
			&quot;padding&quot; : &quot;2px&quot;, &quot;background&quot; : &quot;linear-gradient(to bottom, rgba(69,72,77,1) 0%,rgba(0,0,0,1) 100%)&quot;, &quot;border-radius&quot; : &quot;2px&quot;,
			&quot;box-shadow&quot; : &quot;0px 2px 5px -2px rgba(0,0,0,0.75)&quot;,	&quot;user-select&quot;: &quot;none&quot;, &quot;font-family&quot; : &quot;sans-serif&quot;, &quot;font-size&quot; :&quot;12px&quot;, &quot;text-align&quot; : &quot;center&quot;
		});
		$(toolbox).insertAfter($(this).parent());
		$(toolbox).offset(position);
    	$(toolbox).hide();
        $(toolbox).on(&quot;mousedown.drawr touchstart.drawr&quot;, function(e){
        	var ownerCanvas = this.ownerCanvas;
			var mouse_data = plugin.get_mouse_data.call(ownerCanvas,e,this);
    		$(this).data(&quot;offsetx&quot;, mouse_data.x).data(&quot;offsety&quot;, mouse_data.y).data(&quot;dragging&quot;, true);
    		plugin.is_dragging=true;
    		e.preventDefault();
    	});
		return $(toolbox);
    };

答案1

得分: 1

这是修改过的 JavaScript 函数的一部分,用于创建工具箱。

英文:

I came to this kind possibility too (modifying the orginal js function):

plugin.create_toolbox = function(id, position, title, width) {
  var self = this;
  var toolbox = document.createElement(&quot;div&quot;);
  toolbox.innerHTML = &quot;&lt;div style=&#39;padding:5px 0px 5px 0px&#39;&gt;&quot; + title + &quot;&lt;/div&gt;&quot;;
  toolbox.className = &quot;drawr-toolbox drawr-toolbox-&quot; + id;
  toolbox.ownerCanvas = self;
  $(toolbox).css({
    &quot;position&quot;: &quot;absolute&quot;,
    &quot;z-index&quot;: 6,
    &quot;cursor&quot;: &quot;move&quot;,
    &quot;width&quot;: width + &quot;px&quot;,
    &quot;height&quot;: &quot;auto&quot;,
    &quot;color&quot;: &quot;#fff&quot;,
    &quot;padding&quot;: &quot;2px&quot;,
    &quot;background&quot;: &quot;linear-gradient(to bottom, rgba(69,72,77,1) 0%,rgba(0,0,0,1) 100%)&quot;,
    &quot;border-radius&quot;: &quot;2px&quot;,
    &quot;box-shadow&quot;: &quot;0px 2px 5px -2px rgba(0,0,0,0.75)&quot;,
    &quot;user-select&quot;: &quot;none&quot;,
    &quot;font-family&quot;: &quot;sans-serif&quot;,
    &quot;font-size&quot;: &quot;12px&quot;,
    &quot;text-align&quot;: &quot;center&quot;
  });

  var canvasOffset = $(this).offset();
  var toolboxOffset = {
    top: canvasOffset.top + position.top,
    left: canvasOffset.left + position.left
  };

  $(toolbox).insertAfter(this);
  $(toolbox).hide();
  $(toolbox).offset(toolboxOffset);

  $(toolbox).on(&quot;mousedown.drawr touchstart.drawr&quot;, function(e) {
    var ownerCanvas = this.ownerCanvas;
    var mouse_data = plugin.get_mouse_data.call(ownerCanvas, e, this);
    $(this)
      .data(&quot;offsetx&quot;, mouse_data.x)
      .data(&quot;offsety&quot;, mouse_data.y)
      .data(&quot;dragging&quot;, true);
    plugin.is_dragging = true;
    e.preventDefault();
  });

  return $(toolbox);
};

答案2

得分: 0

已弄清楚,如果有人感兴趣的话。首先,我必须修改主要的库文件,如果是刷子工具箱(我需要的工具),则添加一个ID:

jquery.drawr.combined.js

plugin.create_toolbox = function(id, position, title, width) {
    var self = this;
    var toolbox = document.createElement("div");
    toolbox.innerHTML = "<div style='padding:5px 0px 5px 0px'>" + title + "</div>";
    toolbox.className = "drawr-toolbox drawr-toolbox-" + id;
    if (id == 'brush') {
        toolbox.id = position.top;
    }
    toolbox.ownerCanvas = self;
    $(toolbox).css({
        "position": "absolute", "z-index": 6, "cursor": "move", "width": width + "px", "height": "auto", "color": "#fff",
        "padding": "2px", "background": "linear-gradient(to bottom, rgba(69,72,77,1) 0%,rgba(0,0,0,1) 100%)", "border-radius": "2px",
        "box-shadow": "0px 2px 5px -2px rgba(0,0,0,0.75)", "user-select": "none", "font-family": "sans-serif", "font-size": "12px", "text-align": "center"
    });
    $(toolbox).insertAfter($(this).parent());
    $(toolbox).offset(position);
    $(toolbox).hide();
    $(toolbox).on("mousedown.drawr touchstart.drawr", function(e) {
        var ownerCanvas = this.ownerCanvas;
        var mouse_data = plugin.get_mouse_data.call(ownerCanvas, e, this);
        $(this).data("offsetx", mouse_data.x).data("offsety", mouse_data.y).data("dragging", true);
        plugin.is_dragging = true;
        e.preventDefault();
    });
    return $(toolbox);
};

然后在我的主文件中,在循环中启动了drawr,然后在循环结束后,使用map函数获取了所有的ID。然后循环遍历这些ID,并获取了每个ID的正确偏移量top(left是固定的),并应用了它。

var question_secs = <?php echo json_encode($question_section_arr); ?>;
var arrayLength = question_secs.length;

$(document).ready(function() {

    for (var i = 0; i < arrayLength; i++) {
        var section = question_secs[i];

        $("#drawr-container-" + section + " .demo-canvas-" + section + "").drawr({
            "enable_transparency": false
        });

        $("#drawr-container-" + section + " .demo-canvas-" + section + "").drawr("start");

    }

    var ids_pos = [];
    var ids = $('.drawr-toolbox').map(function() {
        return $(this).attr('id');

    });

    for (var x = 0; x < ids.length; x++) {
        ids_pos.push(ids[x]);
    }

    let unique = [];
    ids_pos.forEach(element => {
        if (!unique.includes(element)) {
            unique.push(element);
        }
    });

    for (var x = 0; x < unique.length; x++) {
        var offset_pos = document.getElementById(unique[x]);
        let str = offset_pos.style.top;
        var correct_top = Number(offset_pos.style.top.substring(0, offset_pos.style.top.length - 2)) + 1127;
        var correct_left = Number(offset_pos.style.left.substring(0, offset_pos.style.left.length - 2)) - 450;
        console.log(correct_top);
        console.log(correct_left);
        console.log(offset_pos);

        offset_pos.classList.add("pos-"+x);

        $(".pos-"+x).css({top: correct_top+"px", left: "80px", position:'absolute'});
    }
});

希望这对你有帮助。

英文:

Figured it out in case anyone is curious. First I had to alter the main library file and if it was the brush toolbox (the one I needed) add an ID:

jquery.drawr.combined.js 
plugin.create_toolbox = function(id,position,title,width){
var self = this;
var toolbox = document.createElement(&quot;div&quot;);
toolbox.innerHTML=&quot;&lt;div style=&#39;padding:5px 0px 5px 0px&#39;&gt;&quot; + title + &quot;&lt;/div&gt;&quot;;
toolbox.className = &quot;drawr-toolbox drawr-toolbox-&quot; + id;
if(id == &#39;brush&#39;) {
toolbox.id = position.top; 
}
toolbox.ownerCanvas = self;
$(toolbox).css({
&quot;position&quot; : &quot;absolute&quot;, &quot;z-index&quot; : 6, &quot;cursor&quot; : &quot;move&quot;, &quot;width&quot; : width + &quot;px&quot;, &quot;height&quot; : &quot;auto&quot;, &quot;color&quot; : &quot;#fff&quot;,
&quot;padding&quot; : &quot;2px&quot;, &quot;background&quot; : &quot;linear-gradient(to bottom, rgba(69,72,77,1) 0%,rgba(0,0,0,1) 100%)&quot;, &quot;border-radius&quot; : &quot;2px&quot;,
&quot;box-shadow&quot; : &quot;0px 2px 5px -2px rgba(0,0,0,0.75)&quot;,	&quot;user-select&quot;: &quot;none&quot;, &quot;font-family&quot; : &quot;sans-serif&quot;, &quot;font-size&quot; :&quot;12px&quot;, &quot;text-align&quot; : &quot;center&quot;
});
$(toolbox).insertAfter($(this).parent());
$(toolbox).offset(position);
$(toolbox).hide();
$(toolbox).on(&quot;mousedown.drawr touchstart.drawr&quot;, function(e){
var ownerCanvas = this.ownerCanvas;
var mouse_data = plugin.get_mouse_data.call(ownerCanvas,e,this);
$(this).data(&quot;offsetx&quot;, mouse_data.x).data(&quot;offsety&quot;, mouse_data.y).data(&quot;dragging&quot;, true);
plugin.is_dragging=true;
e.preventDefault();
});
return $(toolbox);
};

Then on my main file I started the drawr in a loop, then after the loop got all the ids with the map function. Then looped through those and got the correct offset top for each one (left was static) and applied it.

var question_secs = &lt;?php echo json_encode($question_section_arr); ?&gt;;
var arrayLength = question_secs.length;
$(document).ready(function(){
for (var i = 0; i &lt; arrayLength; i++) {
var section = question_secs[i];
$(&quot;#drawr-container-&quot; + section + &quot; .demo-canvas-&quot; + section + &quot;&quot;).drawr({
&quot;enable_transparency&quot;: false
});
$(&quot;#drawr-container-&quot; + section + &quot; .demo-canvas-&quot; + section + &quot;&quot;).drawr(&quot;start&quot;);
}
var ids_pos = [];
var ids = $(&#39;.drawr-toolbox&#39;).map(function() {
return $(this).attr(&#39;id&#39;);
});
for (var x = 0; x &lt; ids.length; x++) {
ids_pos.push(ids[x]);
}
let unique = [];
ids_pos.forEach(element =&gt; {
if (!unique.includes(element)) {
unique.push(element);
}
});
for (var x = 0; x &lt; unique.length; x++) {
var offset_pos = document.getElementById(unique[x]);
let str = offset_pos.style.top;
var correct_top = Number(offset_pos.style.top.substring(0, offset_pos.style.top.length - 2)) + 1127;
var correct_left = Number(offset_pos.style.left.substring(0, offset_pos.style.left.length - 2)) - 450;
console.log(correct_top);
console.log(correct_left);
console.log(offset_pos);
offset_pos.classList.add(&quot;pos-&quot;+x);
$(&quot;.pos-&quot;+x).css({top: correct_top+&quot;px&quot;, left: &quot;80px&quot;, position:&#39;absolute&#39;});
}
});

huangapple
  • 本文由 发表于 2023年7月7日 00:47:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76630973.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定