2014-09-25 337 views
0

我很想創造一個搗鼓它來顯示,但我使用PHP和它不會讓我使用PHP在這些所以我希望有人仍然會知道怎麼回事上!HTML5畫布和PHP在Chrome不工作

我有一個JavaScript,它自己的工作完全罰款。它是一個HTML點擊和拖動畫布。單擊和拖動限制爲一個圓形,並在單擊畫布旁邊的按鈕時將圖像繪製到畫布上。該按鈕調用一種將圖像繪製到畫布上並使其可點擊和拖動的方法。我已經測試了它本身,它的作品非常漂亮。當我添加一段簡單的PHP代碼時,我的點擊和拖動畫布停止移動圖像。當您點擊按鈕繪製圖像時,該圖像可以正常工作,但無法移動圖像。

我超越困惑,因爲我使用的PHP已經無關什麼是在畫布回事。這裏是代碼:

這也是很重要的一點指出,這段代碼在safari中工作正常,但在chrome中根本不工作,所以我知道它與chrome有關我只是不明白是什麼問題是。

我的問題主要是,是有辦法,Safari瀏覽器加載與鉻會影響在同一頁面上運行的JavaScript和PHP的,因爲它在一個瀏覽器正常工作,而不是其他。我只是添加了代碼,以便人們知道我指的是什麼。

這裏是PHP

<dl class="header"> 
<?php 
    $name = $_GET['id']; 
    if ($name=="bracelet") { 
     echo "<li>Design x!</li>"; 
    } 
    elseif ($name=="purse") { 
     echo "<li>Design y!</li>"; 
    } 
    elseif ($name=="ring") { 
     echo "<li>Design z!</li>"; 
    } 
?> 
</dl> 

下面是完整的代碼

<HTML> 
<HEAD> 
<style> 
#canvas { 
    border:1px solid red; 
} 
</style> 

<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script> 
<link rel="stylesheet" type="text/css" href="styles.css"> 
</HEAD> 
<BODY> 
<dl class="header"> 
<?php 
    $name = $_GET['id']; 
    if ($name=="bracelet") { 
     echo "<li>Design x!</li>"; 
    } 
    elseif ($name=="purse") { 
     echo "<li>Design y!</li>"; 
    } 
    elseif ($name=="ring") { 
     echo "<li>Design z!</li>"; 
    } 
?> 
</dl> 

<h5>Add Images and Canvases with the buttons<br> 
Click to select which image to move.<br> 
Then move the mouse to desired drop location<br> 
and click again to drop the image there.</h5> 

<canvas id="canvas" width=300 height=300></canvas> 
<input type="image" src="http://s25.postimg.org/tovdg674b/crystal_003.png" id="button1" width="35"  height="20"></input> 
<input type="image" src="http://s25.postimg.org/ph0l7f5or/crystal_004.png" id="button2" width="35" height="20"></input> 
<input type="image" src="http://s25.postimg.org/60fvkwakr/crystal_005.png" id="button3" width="35" height="20"></input> 
<input type="image" src="http://s25.postimg.org/fz5fl49e3/crystal_006.png" id="button4" width="35" height="20"></input> 
<button id="save">save</button> 
<br> 
<script> 
// canvas stuff 
var canvas = document.getElementById("canvas"); 
var ctx = canvas.getContext("2d"); 
var centerX = canvas.width/2; 
var centerY = canvas.height/2; 
var radius = 50; 

var contexts = []; 
var points = []; 

// image stuff 
var states = []; 
var img = new Image(); 
img.onload = function() {} 
img.src = "http://s25.postimg.org/5qs46n4az/crystal_009.png"; 

setUpCanvas(); 
setUpPoints(); 

function setUpCanvas() { 
    contexts.push(canvas.getContext("2d")); 
    // link the new canvas to its context in the contexts[] array 
    canvas.contextIndex = contexts.length; 
    // wire up the click handler 
    canvas.onclick = function (e) { 
     handleClick(e, this.contextIndex); 
    }; 
    // wire up the mousemove handler 
    canvas.onmousemove = function (e) { 
     handleMousemove(e, this.contextIndex); 
    }; 
    canvas.addEventListener('dblclick', function() { 
         removeState(this.contextIndex); 
         }); 
} 

function setUpPoints() { 
    //points that make up a circle circumference to an array 
    points = []; 
    for (var degree=0; degree<360; degree++) { 
    var radians = degree * Math.PI/180; 
    var TO_RADIANS = Math.PI/180; 
    var xpoint = centerX + radius * Math.cos(radians); 
    var ypoint = centerY + radius * Math.sin(radians); 
    points.push({ 
       x: xpoint, 
       y: ypoint 
       }); 
} 
ctx.beginPath(); 
ctx.moveTo(points[0].x + 4, points[0].y + 4) 
//draws the thin line on the canvas 
for (var i = 1; i < points.length; i++) { 
    var pt = points[i]; 
    ctx.lineTo(pt.x + 4, pt.y + 4); 
} 
ctx.stroke(); //end of drawing the thin line 
} 

function addCircle() { 
    ctx.beginPath(); 
    ctx.moveTo(points[0].x + 4, points[0].y + 4) 
    //draws the thin line on the canvas 
    for (var i = 1; i < points.length; i++) { 
    var pt = points[i]; 
    ctx.lineTo(pt.x + 4, pt.y + 4); 
    } 
    ctx.stroke(); //end of drawing the thin line 
} 

function clearAll() { 
    //Clear all canvases 
    for (var i = 0; i < contexts.length; i++) { 
     var context = contexts[i]; 
     context.clearRect(0, 0, canvas.width, canvas.height); 
    } 
} 

function handleClick(e, contextIndex) { 

    e.stopPropagation(); 

    var mouseX = parseInt(e.clientX - e.target.offsetLeft); 
    var mouseY = parseInt(e.clientY - e.target.offsetTop); 

    for (var i = 0; i < states.length; i++) { 

     var state = states[i]; 
     console.log(state); 

    if (state.dragging) { 
     state.dragging = false; 
     state.draw(); 
     continue; 
    } 
    if (state.contextIndex == contextIndex && mouseX > state.x && mouseX < state.x + state.width && mouseY > state.y && mouseY < state.y + state.height) { 
     state.dragging = true; 
     state.offsetX = mouseX - state.x; 
     state.offsetY = mouseY - state.y; 
     state.contextIndex = contextIndex; 
    } 
    state.draw(); 
    } 
} 

function handleMousemove(e, contextIndex) { 
    e.stopPropagation(); 

    var mouseX = parseInt(e.clientX - e.target.offsetLeft); 
    var mouseY = parseInt(e.clientY - e.target.offsetTop); 
    clearAll(); 
    addCircle(); 
    var minDistance = 1000; 
    var minPoint = -1; 

    for (var i = 0; i < states.length; i++) { 

    var state = states[i]; 

    if (state.dragging) { 
     for (var i = 0; i < points.length; i++) { 
      var pt = points[i]; 
      var dx = mouseX - pt.x; 
      var dy = mouseY - pt.y; 
      if ((dx > 0 && dx>120)) { 
       state.x = mouseX - state.offsetX; 
       state.y = mouseY - state.offsetY; 
       state.contextIndex = contextIndex; 
      } else if ((dx < 0 && dx < -120)) { 
       state.x = mouseX - state.offsetX; 
       state.y = mouseY - state.offsetY; 
       state.contextIndex = contextIndex; 
      } 
      else { 
       var distance = Math.sqrt(dx * dx + dy * dy); 
       if (distance < minDistance) { 
        minDistance = distance; 
        //points in relation to the constrained line (where it will be drawn to) 
        //reset state.x and state.y to closest point on the line 
        state.x = pt.x - img.width/2; 
        state.y = pt.y - img.height/2; 
        state.contextIndex = contextIndex; 
       } 
      } 
     } 

    } 
    state.draw(); 
    } 
} 

function removeState(contextIndex) { 
    for (var i = 0; i < states.length; i++) { 

    var state = states[i]; 
    state.remove(); 
    } 
} 

function addState(image) { 
    var ptxy = points[1]; 
    state = {} 
    state.dragging = false; 
    state.contextIndex = 1; 
    state.image = image; 
    state.x = ptxy.x - image.width/2; 
    state.y = ptxy.y - image.height/2; 
    state.width = image.width; 
    state.height = image.height; 
    state.offsetX = 0; 
    state.offsetY = 0; 
    state.draw = function() { 
    var context = contexts[this.contextIndex - 1]; 
    if (this.dragging) { 
     context.strokeStyle = 'black'; 
     context.strokeRect(this.x, this.y, this.width + 2, this.height + 2) 
    } 
    context.drawImage(this.image, this.x, this.y); 
} 
state.draw(); 
return (state); 
} 
function save() { 
    // var data = ctx.getImageData(0, 0, canvas.width, canvas.height); 

} 

$("#button1").click(function() { 
       states.push(addState(img)); 
       }); 
$("#button2").click(function() { 
       states.push(addState(img)); 
       }); 
$("#button3").click(function() { 
       states.push(addState(img)); 
       }); 
$("#button4").click(function() { 
       states.push(addState(img)); 
       }); 
$("#save").click(function() { 
      save(); 
      }); 


</script> 
</BODY> 
</HTML> 
+0

所以我們應該通讀代碼,牆壁,並試圖猜出/你的「PHP線」是什麼? – 2014-09-25 21:55:05

+1

其中的頂部,說<?php – gikygik 2014-09-25 21:58:20

+0

那麼我修改它,並添加它作爲一個單獨的塊。我也修改了我的問題主要是什麼。我添加了代碼有人想看到它,但我主要想知道是否有一個原因,PHP和點擊和拖動將在safari工作,但不適用於鉻。 – gikygik 2014-09-25 22:07:19

回答

1

任何人都好奇,想知道我是如何解決這個在這裏你去答案。我是HTML5畫布的新手,以及它的工作原理。經過大量的試驗和錯誤之後,我發現一旦畫布從屏幕頂部改變到其他地方,畫布偏移就是錯誤的。它是那樣簡單....