2015-11-02 56 views
0

我使用interact.js庫來編寫這段在Chrome,Firefox和w3schools上獨立運行的代碼「自己動手」(不適用於Edge和IE由於某種原因)。問題是,當我使用layout.xml中的代碼調用template.phtml時,magento只渲染一次,因此用戶不允許調整多維數據集的大小。在模板中使用interact.js的magento動態立方體.phtml

<!-- CSS --> 
<style type="text/css"> 
svg { 
    width: 100%; 
    height: 300px; 
    background-color: #CDC9C9; 

    -ms-touch-action: none; 
     touch-action: none; 
} 
.edit-rectangle { 
    fill: black; 
    stroke: #fff; 
} 
body { margin: 0; } 
</style> 

<!-- Content --> 
<br> 
<svg> 
</svg> 

<br> 
<button onclick="location.href = 'square';" id="previousbutton">Go back</button> 
<button onclick="location.href = 'squaresection';" style="float:right" id="nextButton">Proceed to next step</button> 
<br> 
<br> 

<script type="text/javascript" src="interact.js"> 
</script> 

<!-- JavaScript --> 

<script type="text/javascript"> 

var svgCanvas = document.querySelector('svg'), 
    svgNS = 'http://www.w3.org/2000/svg', 
    rectangles = []; 
    labels = []; 
    rectNumb = 5; 

function Rectangle (x, y, w, h, svgCanvas) { 
    this.x = x; 
    this.y = y; 
    this.w = w; 
    this.h = h; 
    this.stroke = 0; 
    this.el = document.createElementNS(svgNS, 'rect'); 

    this.el.setAttribute('data-index', rectangles.length); 
    this.el.setAttribute('class', 'edit-rectangle'); 
    rectangles.push(this); 

    this.draw(); 
    svgCanvas.appendChild(this.el); 
} 


function Label (x, y, text, svgCanvas){ 
    this.x = x; 
    this.y = y; 
    this.text = text; 
    this.el = document.createElementNS(svgNS, 'text'); 
    labels.push(this); 

    this.draw(); 
    svgCanvas.appendChild(this.el); 
} 
Label.prototype.draw = function() { 
    this.el.setAttribute('x', this.x); 
    this.el.setAttribute('y', this.y); 
    this.el.setAttribute('font-family', "Verdana"); 
    this.el.setAttribute('font-size', 14); 
    this.el.setAttribute('fill', "black"); 
    this.el.innerHTML = this.text; 
} 

Rectangle.prototype.draw = function() { 
    this.el.setAttribute('x', this.x + this.stroke/2); 
    this.el.setAttribute('y', this.y + this.stroke/2); 
    this.el.setAttribute('width' , this.w - this.stroke); 
    this.el.setAttribute('height', this.h - this.stroke); 
    this.el.setAttribute('stroke-width', this.stroke); 
} 

interact('.edit-rectangle') 
    // change how interact gets the 
    // dimensions of '.edit-rectangle' elements 
    .rectChecker(function (element) { 
    // find the Rectangle object that the element belongs to 
    var rectangle = rectangles[element.getAttribute('data-index')]; 
    // return a suitable object for interact.js 
    return { 
     left : rectangle.x, 
     top : rectangle.y, 
     right : rectangle.x + rectangle.w, 
     bottom: rectangle.y + rectangle.h 
    }; 
    }) 
/* 
.draggable({ 
    max: Infinity, 
    onmove: function (event) { 
     var rectangle = rectangles[event.target.getAttribute('data-index')]; 

     rectangle.x += event.dx; 
     rectangle.y += event.dy; 
     rectangle.draw(); 
    } 
    }) 
*/ 
.resizable({ 
    onstart: function (event) {}, 
    onmove : function (event) { 
     if (event.target.getAttribute('data-index') > 0) 
     { 
     // Main Rect 
     var rectangle = rectangles[event.target.getAttribute('data-index')]; 
     var rectangle2 = rectangles[event.target.getAttribute('data-index') - 1]; 

     if (rectangle.w - event.dx > 10 && rectangle2.w + event.dx > 10){ 
      rectangle.x += event.dx; 
      rectangle.w = rectangle.w - event.dx; 

      rectangle2.w = rectangle2.w + event.dx; 
     } 

     rectangle.draw(); 
     rectangle2.draw(); 

     var label = labels[event.target.getAttribute('data-index')]; 
     var label2 = labels[event.target.getAttribute('data-index') - 1]; 
     label.text = rectangle.w + " mm"; 
     label2.text = rectangle2.w + " mm"; 

     label.x = rectangle.x + rectangle.w/4; 
     label2.x = rectangle2.x + rectangle2.w/4; 

     label.draw(); 
     label2.draw(); 
     } 
    }, 
    onend : function (event) {}, 

    edges: { 
     top : false,  // Disable resizing from top edge. 
     left : true,  
     bottom: false,  
     right : false  // Enable resizing on right edge 
    }, 

    inertia: false, 

    // Width and height can be adjusted independently. When `true`, width and 
    // height are adjusted at a 1:1 ratio. 
    square: false, 

    // Width and height can be adjusted independently. When `true`, width and 
    // height maintain the aspect ratio they had when resizing started. 
    preserveAspectRatio: false, 

    // a value of 'none' will limit the resize rect to a minimum of 0x0 
    // 'negate' will allow the rect to have negative width/height 
    // 'reposition' will keep the width/height positive by swapping 
    // the top and bottom edges and/or swapping the left and right edges 
    invert: 'reposition', 

    // limit multiple resizes. 
    // See the explanation in the @Interactable.draggable example 
    max: Infinity, 
    maxPerElement: 3, 
}); 

interact.maxInteractions(Infinity); 

var positionX = 50, 
    positionY = 80, 
    width = 80, 
    height = 80; 

for (var i = 0; i < rectNumb; i++) { 
    positionX = 50 + 82 * i; 
    new Rectangle(positionX, positionY, width, height, svgCanvas); 
} 
for (var i = 0; i < rectNumb; i++) { 
    positionX = 50 + 82 * i; 
    new Label(positionX + width/4, positionY + height + 20, width +" mm", svgCanvas); 
} 
</script> 

任何關於我能做些什麼來實現這個代碼到magento的建議將不勝感激。

回答

0

Magento沒有呈現代碼只有一次。問題在於canvas事件監聽器總是認爲指針座標是錯誤的。由於canvas是頁面的第一個元素(因爲它是該.phtml文件中的第一個元素),所以事件偵聽器假定它將顯示在頂部,但由於magento頁面呈現的工作方式,情況並非如此。

簡單地通過測量畫布上方內容的高度,並在將數據傳遞給事件偵聽器之前,從指針位置減去該高度來解決此問題。

該解決方案的問題在於,它僅適用於單頁或多頁具有高於畫布的內容高度(=>相同設計)的頁面。如果有人知道一個人不需要「重新計算」具有不同設計的每個頁面的高度,共享知識將非常感激。

相關問題