简体   繁体   English

html5 Canvas在画布中显示默认区域

[英]html5 Canvas display default area in canvas

I would like to display default my "Dedication Text" in the middle of my canvas. 我想在画布中间显示默认的“奉献文本”。 I really can't determine the coordinates nor understand the codes... 我真的无法确定坐标也无法理解代码...

Currently my Dedication Text displays here : 目前,我的奉献文本显示在这里: 当前显示

As you can see, it displays on the left top corner of the canvas. 如您所见,它显示在画布的左上角。

Where I would like to automatically display is in the middle Like this: 我想自动显示的位置在中间,就像这样: 期望的例子

I have this code that I got from net: 我有从网上得到的这段代码:

  function updateTotal() { if (document.getElementById('design3').checked) { var canvas2 = document.getElementById("displaycake_text"); context = canvas2.getContext("2d"); var $canvas2 = $("#displaycake_text"); var canvasOffset = $canvas2.offset(); var offsetX = canvasOffset.left; var offsetY = canvasOffset.top; var scrollX = $canvas2.scrollLeft(); var scrollY = $canvas2.scrollTop(); var startX; var startY; var texts = []; // an array to hold text objects var selectedText = -1;// this var will hold the index of the hit-selected text function draw() { // clear the canvas & redraw all texts context.clearRect(0, 0, canvas2.width, canvas2.height); for (var i = 0; i < texts.length; i++) { var text = texts[i]; context.fillText(text.text, text.x, text.y); } } function textHittest(x, y, textIndex) { // test if x,y is inside the bounding box of texts[textIndex] var text = texts[textIndex]; return (x >= text.x && x <= text.x + text.width && y >= text.y - text.height && y <= text.y); } function handleMouseDown(d) { d.preventDefault(); startX = parseInt(d.clientX - offsetX); startY = parseInt(d.clientY - offsetY); for (var i = 0; i < texts.length; i++) { if (textHittest(startX, startY, i)) { selectedText = i; } } } function handleMouseUp(d) { // done dragging d.preventDefault(); selectedText = -1; } function handleMouseOut(d) { // also done dragging d.preventDefault(); selectedText = -1; } function handleMouseMove(d) { if (selectedText < 0) { return; } d.preventDefault(); mouseX = parseInt(d.clientX - offsetX); mouseY = parseInt(d.clientY - offsetY); var dx = mouseX - startX; var dy = mouseY - startY; startX = mouseX; startY = mouseY; var text = texts[selectedText]; text.x += dx; text.y += dy; draw(); } $("#displaycake_text").mousedown(function (d) { handleMouseDown(d); }); // listen for mouse events $("#displaycake_text").mousemove(function (d) { handleMouseMove(d); }); $("#displaycake_text").mouseup(function (d) { handleMouseUp(d); }); $("#displaycake_text").mouseout(function (d) { handleMouseOut(d); }); $("#text_dedi").click(function () { var y = texts.length * 20 + 20; var text = { text: $("#dedi_text").val(), x: 20, y: y }; context.font = "30px Roboto"; text.width = context.measureText(text.text).width; text.height = 16; text.color = "#ffffff"; texts.push(text); // put this new text in the texts array draw(); // redraw everything }); //this is the code for CLEAR BUTTON document.getElementById('clear').addEventListener('click', function() { context.clearRect(0, 0, canvas2.width, canvas2.height); texts = []; }, false); } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <canvas id="displaycake_text" height="300px" width="600px"> </canvas> <!-- CLICK THE RADIO TO TRIGGER POST --> <input type="radio" id="design3" name="design_3" onchange="updateTotal()" /> Dedication <h2> <div class="disp_dedi off"> <input type="text" size="15" id="dedi_text" name="dedicationT" placeholder="Dedication"> <button id="text_dedi"> Post </button> <input type="button" value="Clear" id="clear" size="23" onchange="updateTotal()"> 

As you can see, I can drag it around... But I'm going to add some features that would decline the dragging part. 如您所见,我可以拖动它...但是我将添加一些功能来减少拖动部分。

Can anyone help me point out which in the JS code that I could set the place of the text to be posted by default somewhere in the middle? 谁能帮我指出我可以在JS代码中的哪个位置设置文本的默认位置? The source doesn't have enough comments for me to understand. 消息来源没有足够的评论让我理解。

THANK YOU IN ADVANCE!!! 先感谢您!!!

First you need to align the text that you are trying to draw on the canvas by using textAlign property of canvas. 首先,您需要使用canvas的textAlign属性来对齐要在画布上绘制的文本。
Second you need to set the fillText 's x , y coordinates according to half of the canvas width / height, so that you can place the text in the middle of the canvas. 其次,您需要根据画布宽度/高度的一半设置fillTextxy坐标,以便可以将文本放置在画布的中间。 So, basically you have to add / change just 2 lines of code in your already existing snippet and that would be : 因此,基本上,您只需要在现有片段中添加/更改两行代码即可:

context.textAlign = 'center';
context.fillText(text.text, canvas2.width / 2, canvas2.height / 2);

and the good thing is it automatically removes the dragging part. 好处是它会自动删除拖动部分。

 function updateTotal() { if (document.getElementById('design3').checked) { var canvas2 = document.getElementById("displaycake_text"); context = canvas2.getContext("2d"); var $canvas2 = $("#displaycake_text"); var canvasOffset = $canvas2.offset(); var offsetX = canvasOffset.left; var offsetY = canvasOffset.top; var scrollX = $canvas2.scrollLeft(); var scrollY = $canvas2.scrollTop(); var startX; var startY; var texts = []; // an array to hold text objects var selectedText = -1; // this var will hold the index of the hit-selected text function draw() { // clear the canvas & redraw all texts context.clearRect(0, 0, canvas2.width, canvas2.height); for (var i = 0; i < texts.length; i++) { var text = texts[i]; context.textAlign = 'center'; context.fillText(text.text, canvas2.width / 2, canvas2.height / 2); } } function textHittest(x, y, textIndex) { // test if x,y is inside the bounding box of texts[textIndex] var text = texts[textIndex]; return (x >= text.x && x <= text.x + text.width && y >= text.y - text.height && y <= text.y); } function handleMouseDown(d) { d.preventDefault(); startX = parseInt(d.clientX - offsetX); startY = parseInt(d.clientY - offsetY); for (var i = 0; i < texts.length; i++) { if (textHittest(startX, startY, i)) { selectedText = i; } } } function handleMouseUp(d) { // done dragging d.preventDefault(); selectedText = -1; } function handleMouseOut(d) { // also done dragging d.preventDefault(); selectedText = -1; } function handleMouseMove(d) { if (selectedText < 0) { return; } d.preventDefault(); mouseX = parseInt(d.clientX - offsetX); mouseY = parseInt(d.clientY - offsetY); var dx = mouseX - startX; var dy = mouseY - startY; startX = mouseX; startY = mouseY; var text = texts[selectedText]; text.x += dx; text.y += dy; draw(); } $("#displaycake_text").mousedown(function(d) { handleMouseDown(d); }); // listen for mouse events $("#displaycake_text").mousemove(function(d) { handleMouseMove(d); }); $("#displaycake_text").mouseup(function(d) { handleMouseUp(d); }); $("#displaycake_text").mouseout(function(d) { handleMouseOut(d); }); $("#text_dedi").click(function() { var y = texts.length * 20 + 20; var text = { text: $("#dedi_text").val(), x: 20, y: y }; context.font = "30px Roboto"; text.width = context.measureText(text.text).width; text.height = 16; text.color = "#ffffff"; texts.push(text); // put this new text in the texts array draw(); // redraw everything }); //this is the code for CLEAR BUTTON document.getElementById('clear').addEventListener('click', function() { context.clearRect(0, 0, canvas2.width, canvas2.height); texts = []; }, false); } } 
 #displaycake_text { background-color: lightgrey; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <canvas id="displaycake_text" height="300px" width="600px"> </canvas> <!-- CLICK THE RADIO TO TRIGGER POST --> <input type="radio" id="design3" name="design_3" onchange="updateTotal()" /> Dedication <h2> <div class="disp_dedi off"> <input type="text" size="15" id="dedi_text" name="dedicationT" placeholder="Dedication"> <script src="https://code.jquery.com/jquery-3.1.0.js"></script> <button id="text_dedi"> Post </button> <input type="button" value="Clear" id="clear" size="23" onchange="updateTotal()"> 

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

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