0
0
mirror of https://github.com/Doodle3D/doodle3d-client.git synced 2024-11-14 06:47:55 +01:00
doodle3d-client/js/WordArt.js

157 lines
3.9 KiB
JavaScript
Raw Normal View History

2014-01-16 21:30:57 +01:00
var wordArtPopup;
2014-01-09 17:05:03 +01:00
function initWordArt() {
$("body").append('<div id="svgfont" style="display:none"></div>');
$("#svgfont").load("img/font.svg?");
2014-01-16 21:30:57 +01:00
wordArtPopup = new Popup($("#popupWordArt"),$("#popupMask"));
$("#btnWordArtOk").on("onButtonClick",wordArtPopup.commit);
$("#btnWordArtCancel").on("onButtonClick",wordArtPopup.cancel);
$("#popupWordArt").bind("onPopupCancel", onWordArtCancel);
$("#popupWordArt").bind("onPopupCommit", onWordArtOk);
2014-01-09 17:05:03 +01:00
}
function showWordArtDialog() {
buttonGroupAdd.hide();
2014-01-16 21:30:57 +01:00
wordArtPopup.open();
2014-01-10 17:21:43 +01:00
$("#txtWordArt").focus();
$("#txtWordArt").val(""); //clear textbox
2014-01-09 17:05:03 +01:00
}
function onWordArtCancel() {
$("#txtWordArt").blur();
2014-01-09 17:05:03 +01:00
}
function onWordArtOk() {
$("#txtWordArt").blur();
var s = $("#txtWordArt").val();
drawTextOnCanvas(s);
2014-01-15 20:16:46 +01:00
}
function drawTextOnCanvas(text) {
if (typeof(text) == 'string') {
var points = getStringAsPoints(text);
2014-01-09 17:05:03 +01:00
2014-01-15 20:16:46 +01:00
var bounds = getBounds(points);
var scaleX = (canvasWidth-50) / bounds.width;
var scaleY = (canvasHeight-50) / bounds.height;
2014-01-09 17:05:03 +01:00
2014-01-15 20:16:46 +01:00
var scale = Math.min(scaleX,scaleY);
2014-01-09 17:05:03 +01:00
2014-01-15 20:16:46 +01:00
scalePoints(points,scale);
var bounds = getBounds(points);
translatePoints(points,-bounds.x,-bounds.y); //left top of text is (0,0)
translatePoints(points,-bounds.width/2,-bounds.height/2); //anchor point center
translatePoints(points,canvasWidth/2,canvasHeight/2); //center in canvas
2014-01-09 17:05:03 +01:00
2014-01-15 20:16:46 +01:00
canvasDrawPoints(canvas,points);
}
2014-01-09 17:05:03 +01:00
}
function getStringAsPoints(text) {
var allPoints = [];
var xPos = 0;
for (var i=0; i<text.length; i++) {
if (text[i]==" ") { //space
xPos += 8;
} else { //other characters
var path = getPathFromChar(text[i]);
var points = getPointsFromPath(path);
if (points.length==0) continue;
translatePoints(points,-points[0][0],0);
var bounds = getBounds(points);
translatePoints(points,-bounds.x,0);
translatePoints(points,xPos,0);
xPos+=bounds.width;
xPos+=2;
for (var j=0; j<points.length; j++) {
allPoints.push(points[j]);
}
}
}
return allPoints;
}
function getPathFromChar(ch) {
var index = ch.charCodeAt(0)-33;
var element = $("#svgfont path")[index];
if (element==undefined) return "";
return $("#svgfont path")[index].attributes["d"].value; //was nodeValue but that's depricated
2014-01-09 17:05:03 +01:00
}
function getPointsFromPath(path) {
var points = [];
var cmds = path.split(' ');
var cursor = { x:0.0, y:0.0 };
var move = false;
2014-01-15 13:49:38 +01:00
var prevCmd = "";
var lastCmd = "";
//console.log(path);
2014-01-09 17:05:03 +01:00
for (var i=0; i<cmds.length; i++) {
2014-01-15 13:49:38 +01:00
var cmd = cmds[i];
var xy = cmd.split(",");
if (cmd.length==1) { //we suppose this is a alpha numeric character and threat it as a command
lastCmd = cmd;
}
move = (lastCmd=='m' || lastCmd=='M');
if (xy.length==2) {
var x = parseFloat(xy[0]);
var y = parseFloat(xy[1]);
if (lastCmd=='m' || lastCmd=='l') { //relative movement
cursor.x += x;
cursor.y += y;
}
else if (lastCmd=='M' || lastCmd=='L') { //absolute movement
cursor.x = x;
cursor.y = y;
}
if (lastCmd=='m') lastCmd='l'; //the next command after a relative move is relative line if not overruled
if (lastCmd=='M') lastCmd='L'; //same for absolute moves
points.push([cursor.x,cursor.y,move]);
2014-01-15 13:49:38 +01:00
} else if (prevCmd == "h"){
cursor.x += parseFloat(cmd);
points.push([cursor.x,cursor.y,move]);
} else if (prevCmd == "v"){
cursor.y += parseFloat(cmd);
points.push([cursor.x,cursor.y,move]);
} else if (prevCmd == "H"){
cursor.x = parseFloat(cmd);
points.push([cursor.x,cursor.y,move]);
} else if (prevCmd == "V"){
cursor.y = parseFloat(cmd);
points.push([cursor.x,cursor.y,move]);
}
prevCmd = cmd;
2014-01-09 17:05:03 +01:00
}
return points;
}
function canvasDrawPoints(canvas,points) {
beginShape();
for (var i=0; i<points.length; i++) {
var p = points[i];
if (points[i][2]) shapeMoveTo(p[0],p[1]);
else shapeLineTo(p[0],p[1]);
}
endShape();
}