简体   繁体   English

为什么我的代码在jsfiddle上工作,而不是我的HTML文件

[英]Why is my code working on jsfiddle but not my HTML file

I am using jsfiddle to store some coordinates. 我正在使用jsfiddle来存储一些坐标。 The coordinates gets displayed on jsfiddle but when I copy it on my local files the coordinates don't get displayed. 坐标显示在jsfiddle上,但是当我将其复制到我的本地文件时,坐标不会显示。 I would like to display the coordinates of that line on my local files, How can this be done? 我想在我的本地文件上显示该行的坐标,如何做到这一点?

This is my HTML file 这是我的HTML文件

<canvas id="canvas" width="300" height="300" style="border: 1px solid black;">     </canvas>  
<div id="coord"></div>  
<div id="coords"></div>  

This is my Javascript File 这是我的Javascript文件

var canvas = document.getElementById('canvas'),  
coord = document.getElementById('coord'),
ctx = canvas.getContext('2d'), // get 2D context
imgCat = new Image(),
arr = [];

imgCat.src = ''http://c.wearehugh.com/dih5/openclipart.org_media_files_johnny_automatic_1360.png';
imgCat.onload = function() { // wait for image load
ctx.drawImage(imgCat, 0, 0); // draw imgCat on (0, 0)
}; 

var mousedown = false;
ctx.strokeStyle = '#0000FF';
ctx.lineWidth = 5;
canvas.onmousedown = function(e) {
arr = [];
var pos = fixPosition(e, canvas);
mousedown = true;
ctx.beginPath();
ctx.moveTo(pos.x, pos.y);
return false;
};

canvas.onmousemove = function(e) {
var pos = fixPosition(e, canvas);
coord.innerHTML = '(' + pos.x + ',' + pos.y + ')';
if (mousedown) {
ctx.lineTo(pos.x, pos.y);
ctx.stroke();
arr.push([pos.x, pos.y])  
}
};
canvas.onmouseup = function(e) {
mousedown = false;
$('#coords').html(JSON.stringify(arr, null, 2));
};

function fixPosition(e, gCanvasElement) {
var x;
var y;
if (e.pageX || e.pageY) { 
x = e.pageX;
y = e.pageY;
}
else { 
x = e.clientX + document.body.scrollLeft +
document.documentElement.scrollLeft;
y = e.clientY + document.body.scrollTop +
document.documentElement.scrollTop;
} 
x -= gCanvasElement.offsetLeft;
y -= gCanvasElement.offsetTop;
return {x: x, y:y};
}

Demo Here 在这里演示

Copy this in a html page. 在html页面中复制它。 The probleme was your link imgCat.src = '' http://c.wearehugh.com/dih5/openclipart.org_media_files_johnny_automatic_1360.png '; 问题是你的链接imgCat.src ='' http: //c.wearehugh.com/dih5/openclipart.org_media_files_johnny_automatic_1360.png'; You put two '' 你把两个''

<html>
<canvas id="canvas" width="300" height="300" style="border: 1px solid black;"></canvas>  
<div id="coord"></div>  
<div id="coords"></div> 
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js</script> 
</html>

<script type="text/javascript">
var canvas = document.getElementById('canvas'),  
coord = document.getElementById('coord'),
ctx = canvas.getContext('2d'), // get 2D context
imgCat = new Image(),
arr = [];

imgCat.src ='http://www.clipartbest.com/cliparts/bTy/E5x/bTyE5xLjc.png';
imgCat.onload = function() { // wait for image load
ctx.drawImage(imgCat, 0, 0); // draw imgCat on (0, 0)
}; 

var mousedown = false;
ctx.strokeStyle = '#0000FF';
ctx.lineWidth = 5;
canvas.onmousedown = function(e) {
arr = [];
var pos = fixPosition(e, canvas);
mousedown = true;
ctx.beginPath();
ctx.moveTo(pos.x, pos.y);
return false;
};

canvas.onmousemove = function(e) {
var pos = fixPosition(e, canvas);
coord.innerHTML = '(' + pos.x + ',' + pos.y + ')';
if (mousedown) {
ctx.lineTo(pos.x, pos.y);
ctx.stroke();
arr.push([pos.x, pos.y])  
}
};
canvas.onmouseup = function(e) {
mousedown = false;
$('#coords').html(JSON.stringify(arr, null, 2));
};

function fixPosition(e, gCanvasElement) {
var x;
var y;
if (e.pageX || e.pageY) { 
x = e.pageX;
y = e.pageY;
}
else { 
x = e.clientX + document.body.scrollLeft +
document.documentElement.scrollLeft;
y = e.clientY + document.body.scrollTop +
document.documentElement.scrollTop;
} 
x -= gCanvasElement.offsetLeft;
y -= gCanvasElement.offsetTop;
return {x: x, y:y};
}
</script>

some configuration is applying to jsfiddle automatically and you need to apply them by your hands. 一些配置自动应用于jsfiddle,您需要手动应用它们。

first you need to add jQuery to you site. 首先,您需要将jQuery添加到您的站点。 Add this line between <head> </head> tags: <head> </head>标记之间添加此行:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

second you need to check that you script executes for example when page loads. 第二,你需要检查你的脚本执行,例如页面加载时。 you need to put you code into this: 你需要把代码放到这个:

$(function() {
// You script here
});

or put it just before </body> tag 或者把它放在</body>标签之前

so in output you jquery code will be like this: 所以在输出中你的jquery代码将是这样的:

<script>
$(function() {
var canvas = document.getElementById('canvas'),  
coord = document.getElementById('coord'),
ctx = canvas.getContext('2d'), // get 2D context
imgCat = new Image(),
arr = [];

imgCat.src = ''http://c.wearehugh.com/dih5/openclipart.org_media_files_johnny_automatic_1360.png';
imgCat.onload = function() { // wait for image load
ctx.drawImage(imgCat, 0, 0); // draw imgCat on (0, 0)
}; 

var mousedown = false;
ctx.strokeStyle = '#0000FF';
ctx.lineWidth = 5;
canvas.onmousedown = function(e) {
arr = [];
var pos = fixPosition(e, canvas);
mousedown = true;
ctx.beginPath();
ctx.moveTo(pos.x, pos.y);
return false;
};

canvas.onmousemove = function(e) {
var pos = fixPosition(e, canvas);
coord.innerHTML = '(' + pos.x + ',' + pos.y + ')';
if (mousedown) {
ctx.lineTo(pos.x, pos.y);
ctx.stroke();
arr.push([pos.x, pos.y])  
}
};
canvas.onmouseup = function(e) {
mousedown = false;
$('#coords').html(JSON.stringify(arr, null, 2));
};

function fixPosition(e, gCanvasElement) {
var x;
var y;
if (e.pageX || e.pageY) { 
x = e.pageX;
y = e.pageY;
}
else { 
x = e.clientX + document.body.scrollLeft +
document.documentElement.scrollLeft;
y = e.clientY + document.body.scrollTop +
document.documentElement.scrollTop;
} 
x -= gCanvasElement.offsetLeft;
y -= gCanvasElement.offsetTop;
return {x: x, y:y};
}
});
</script>

1.Check the doc type of the HTML :should be html for (HTML 5)or no doc type 1.检查HTML的doc类型:应该是html for(HTML 5)或没有doc类型

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

2.Make sure your scripts are getting loaded properly. 2.确保您的脚本正确加载。 Check the path of Jquery script. 检查Jquery脚本的路径。 --Externally downloaded scripts need to be unblocked (Righ Click -> Properties -> (General Tab)'Unblock' Option on bottom) (Right Click -> Properties -> (general Tab) -> Advanced -> Uncheck Encrypt option if checked.) - 需要取消阻止外部下载的脚本(Righ Click - >属性 - >(常规选项卡)底部的“取消阻止”选项)(右键单击 - >属性 - >(常规选项卡) - >高级 - >取消选中加密选项(如果选中) 。)

3.Put your code inside a function. 3.将代码放在函数中。 (Specifically the binding related codes.) Other functions need to be defined outside document ready.(Already done) And Call that function inside document ready. (特别是绑定相关代码。)其他功能需要在文档准备好之外定义。(已经完成)并且在文档内部调用该函数准备就绪。

$(document).ready(function () {
  DrawImage();
});

function DrawImage()
{
  //your code here
  var canvas = document.getElementById('canvas'),
    coord = document.getElementById('coord'),
    ctx = canvas.getContext('2d'), // get 2D context
    imgCat = new Image(),
    arr = [];

/*********** draw image *************/
imgCat.src = 'http://c.wearehugh.com/dih5/openclipart.org_media_files_johnny_automatic_1360.png';
imgCat.onload = function() { // wait for image load
    ctx.drawImage(imgCat, 0, 0); // draw imgCat on (0, 0)
};

/*********** handle mouse events on canvas **************/
var mousedown = false;
ctx.strokeStyle = '#0000FF';
ctx.lineWidth = 5;
canvas.onmousedown = function(e) {
    arr = [];
    var pos = fixPosition(e, canvas);
    mousedown = true;
    ctx.beginPath();
    ctx.moveTo(pos.x, pos.y);
    return false;
};

canvas.onmousemove = function(e) {
    var pos = fixPosition(e, canvas);
    coord.innerHTML = '(' + pos.x + ',' + pos.y + ')';
    if (mousedown) {
        ctx.lineTo(pos.x, pos.y);
        ctx.stroke();
        arr.push([pos.x, pos.y])
    }
};

canvas.onmouseup = function(e) {
    mousedown = false;
    $('#coords').html(JSON.stringify(arr, null, 2));
};

} }

//Utils
function fixPosition(e, gCanvasElement) {
  //put codes of this function here.
}

只需在body标记结束前添加脚本,您的问题就会解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM