2013-08-21 17:37:20 +02:00
|
|
|
//*
|
|
|
|
var $preview;
|
|
|
|
var preview;
|
|
|
|
var previewCtx;
|
|
|
|
|
|
|
|
var preview_tmp;
|
|
|
|
var previewCtx_tmp;
|
|
|
|
|
2013-09-17 13:09:54 +02:00
|
|
|
var previewDefaults = {
|
2013-12-05 11:14:12 +01:00
|
|
|
rotation: 0, //Math.PI/90,
|
2013-09-17 13:09:54 +02:00
|
|
|
numLayers: 10
|
|
|
|
}
|
|
|
|
|
2013-08-21 17:37:20 +02:00
|
|
|
var svgPathRegExp = /[LM]\d* \d*/ig;
|
|
|
|
var svgPathParamsRegExp = /([LM])(\d*) (\d*)/;
|
|
|
|
|
|
|
|
var prevRedrawTime = new Date().getTime();
|
|
|
|
var redrawInterval = 1000 / 30; // ms
|
|
|
|
|
|
|
|
function initPreviewRendering() {
|
|
|
|
console.log("f:initPreviewRendering()");
|
|
|
|
|
2013-10-11 11:42:27 +02:00
|
|
|
$preview = $("#preview");
|
|
|
|
preview = $preview[0];
|
|
|
|
previewCtx = preview.getContext('2d');
|
|
|
|
|
2013-08-21 17:37:20 +02:00
|
|
|
// DEBUG --> mbt preview_tmp (voor de toImageData truc)
|
|
|
|
var _ratio = preview.width / canvas.width;
|
|
|
|
preview_tmp = document.getElementById('preview_tmp');
|
|
|
|
preview_tmp.width = preview.width;
|
|
|
|
preview_tmp.height = canvas.height * _ratio;
|
|
|
|
$("#preview_tmp").css("top", -preview_tmp.height);
|
|
|
|
|
|
|
|
previewCtx_tmp = preview_tmp.getContext('2d');
|
|
|
|
|
2013-10-29 22:08:08 +01:00
|
|
|
// doodleImageCapture = new Image();
|
|
|
|
|
2013-09-27 18:15:33 +02:00
|
|
|
calcPreviewCanvasProperties();
|
|
|
|
redrawPreview();
|
2013-10-29 22:08:08 +01:00
|
|
|
|
|
|
|
// needed to
|
|
|
|
// doodleImageCapture = new Image();
|
2013-09-27 18:15:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function calcPreviewCanvasProperties() {
|
2013-10-29 21:27:17 +01:00
|
|
|
// console.log("f:calcPreviewCanvasProperties()");
|
2013-09-27 18:15:33 +02:00
|
|
|
|
2013-10-11 13:42:12 +02:00
|
|
|
globalScale = preview.width / canvasWidth;
|
2013-08-21 17:37:20 +02:00
|
|
|
layerCX = (canvasWidth / 2) * globalScale; // defined in canvasDrawing_v01.js
|
|
|
|
layerCY = (canvasHeight / 2) * globalScale; // defined in canvasDrawing_v01.js
|
2013-10-11 13:42:12 +02:00
|
|
|
// layerOffsetY = preview.height - 1.75 * layerCY;
|
|
|
|
layerOffsetY = preview.height * (1 - previewVerticalPadding.bottom);
|
|
|
|
yStep = (preview.height - (preview.height * (previewVerticalPadding.top + previewVerticalPadding.bottom))) / maxNumLayers;
|
2013-08-21 17:37:20 +02:00
|
|
|
}
|
|
|
|
|
2013-09-17 13:09:54 +02:00
|
|
|
// TODO (perhaps) : make the twist limit dynamic, depending on what's printable (w.r.t. overlapping)
|
2013-09-18 20:42:32 +02:00
|
|
|
var previewRotationLimit = Math.PI / 30; // rough estimate
|
2013-09-17 13:09:54 +02:00
|
|
|
|
|
|
|
var numLayers = previewDefaults.numLayers; // current number of preview layers
|
2013-09-07 16:09:09 +02:00
|
|
|
var maxNumLayers= 100; // maximum number of preview layers
|
|
|
|
var minNumLayers= 2; // minimum number of preview layers
|
2013-08-21 17:37:20 +02:00
|
|
|
var globalScale = 0.3; // global scale of preview (width preview / width canvas)
|
2013-09-07 16:09:09 +02:00
|
|
|
var globalAlpha = 0.20; // global alpha of preview
|
|
|
|
var scaleY = 0.4; // additional vertical scale per path for 3d effect
|
|
|
|
var viewerScale = 0.65; // additional scale to fit into preview nicely (otherwise is fills out totally)
|
2013-10-11 13:42:12 +02:00
|
|
|
var previewVerticalPadding = { "top" : .15, "bottom" : 0.12 }; // %
|
2013-09-07 16:09:09 +02:00
|
|
|
var strokeWidth = 2; //4;
|
2013-08-21 17:37:20 +02:00
|
|
|
//var rStep = Math.PI/40; //Math.PI/40; //
|
2013-09-17 13:09:54 +02:00
|
|
|
var rStep = previewDefaults.rotation; // Math.PI/180; //Math.PI/40; //
|
2013-08-21 17:37:20 +02:00
|
|
|
var yStep;// = preview.height / 150; // 3; //6;
|
|
|
|
//var svgWidth = 500; // 650 //parseInt($(svg).css("width"));
|
|
|
|
//var svgHeight = 450; //450; //parseInt($(svg).css("height"));
|
|
|
|
var layerCX, layerCY;
|
|
|
|
//var layerCX = (canvasWidth / 2) * globalScale; // defined in canvasDrawing_v01.js
|
|
|
|
//var layerCY = (canvasHeight / 2) * globalScale; // defined in canvasDrawing_v01.js
|
|
|
|
var layerOffsetY; //= preview.height - 1.75 * layerCY; // 330; // previewHeight - 120
|
|
|
|
var prevX = 0;
|
|
|
|
var prevY = 0;
|
|
|
|
var highlight = true; //highlight bottom, middle and top layers
|
|
|
|
|
|
|
|
var linesRaw = "";
|
|
|
|
var debug_redrawSimplification = 6;
|
|
|
|
function redrawPreview(redrawLess) {
|
|
|
|
if (redrawLess == undefined) redrawLess = false;
|
|
|
|
|
2013-12-11 22:21:05 +01:00
|
|
|
if (_points.length < 2) {
|
|
|
|
previewCtx.clearRect(0, 0, preview.width, preview.height);
|
|
|
|
return;
|
|
|
|
}
|
2013-08-21 17:37:20 +02:00
|
|
|
|
|
|
|
if (!redrawLess) {
|
|
|
|
//debug_redrawSimplification = Math.round(_points.length / 65);
|
|
|
|
//*
|
|
|
|
if (_points.length < 100) {
|
|
|
|
debug_redrawSimplification = 6;
|
|
|
|
} else if (_points.length < 250) {
|
|
|
|
debug_redrawSimplification = 7;
|
|
|
|
} else if (_points.length < 400) {
|
|
|
|
debug_redrawSimplification = 8;
|
|
|
|
} else if (_points.length < 550) {
|
|
|
|
debug_redrawSimplification = 9;
|
|
|
|
} else if (_points.length < 700) {
|
|
|
|
debug_redrawSimplification = 10;
|
|
|
|
} else {
|
|
|
|
debug_redrawSimplification = 11;
|
|
|
|
}
|
|
|
|
//*/
|
|
|
|
// console.log("debug_redrawSimplification: " + debug_redrawSimplification);
|
|
|
|
}
|
|
|
|
|
|
|
|
var y = 0;
|
|
|
|
var r = 0;
|
|
|
|
|
|
|
|
//preview.width = preview.width;
|
|
|
|
previewCtx.clearRect(0, 0, preview.width, preview.height);
|
|
|
|
previewCtx.lineWidth = strokeWidth;
|
|
|
|
previewCtx.strokeStyle = '#f00'; //"rgba(255,255,0,0)";
|
|
|
|
|
|
|
|
for(var i = 0; i < numLayers; i++) {
|
|
|
|
|
2013-09-18 22:35:38 +02:00
|
|
|
var verticalScaleFactor = scaleFunction(i / maxNumLayers);
|
2013-08-21 17:37:20 +02:00
|
|
|
|
|
|
|
if(i == 0 || i == Math.floor(numLayers/2) || i == numLayers-1) {
|
|
|
|
previewCtx.globalAlpha = 1;
|
|
|
|
} else {
|
|
|
|
previewCtx.globalAlpha = globalAlpha;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (redrawLess && i%debug_redrawSimplification != 0 && !(i == 0 || i == Math.floor(numLayers/2) || i == numLayers-1) ) {
|
|
|
|
y -= yStep;
|
|
|
|
r += rStep;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
previewCtx.save();
|
|
|
|
|
2013-10-11 13:42:12 +02:00
|
|
|
// previewCtx.translate(layerCX, layerOffsetY + layerCY + y);
|
|
|
|
previewCtx.translate(layerCX, layerOffsetY + y);
|
2013-08-21 17:37:20 +02:00
|
|
|
// previewCtx.setTransform(1, 0, 0, scaleY, layerCX, layerOffsetY+layerCY+y);
|
2013-09-18 22:35:38 +02:00
|
|
|
previewCtx.scale(viewerScale * verticalScaleFactor, scaleY * viewerScale * verticalScaleFactor);
|
2013-08-21 17:37:20 +02:00
|
|
|
previewCtx.rotate(r);
|
|
|
|
previewCtx.translate((-doodleTransform[0]) * (globalScale * doodleTransform[2]), (-doodleTransform[1]) * (globalScale * doodleTransform[3]));
|
|
|
|
|
|
|
|
var adjustedDoodlePoint = centeredAndScaledDoodlePoint(_points[0]);
|
|
|
|
|
|
|
|
previewCtx.beginPath();
|
|
|
|
previewCtx.moveTo(adjustedDoodlePoint.x, adjustedDoodlePoint.y);
|
|
|
|
for(var j = 1; j < _points.length; j++) {
|
|
|
|
adjustedDoodlePoint = centeredAndScaledDoodlePoint(_points[j])
|
|
|
|
if (redrawLess && j%debug_redrawSimplification != 0 ) continue;
|
|
|
|
previewCtx.lineTo(adjustedDoodlePoint.x, adjustedDoodlePoint.y);
|
|
|
|
}
|
|
|
|
previewCtx.stroke();
|
|
|
|
|
|
|
|
y -= yStep;
|
|
|
|
r += rStep;
|
|
|
|
previewCtx.restore();
|
|
|
|
}
|
|
|
|
previewCtx.globalAlpha = globalAlpha;
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderToImageDataPreview() {
|
|
|
|
if (_points.length < 2) return;
|
|
|
|
|
|
|
|
//*
|
|
|
|
// the first step
|
|
|
|
previewCtx_tmp.clearRect(0, 0, preview.width, preview.height);
|
|
|
|
previewCtx_tmp.lineWidth = strokeWidth;
|
|
|
|
previewCtx_tmp.strokeStyle = '#f00'; //"rgba(255,255,0,0)";
|
|
|
|
|
|
|
|
previewCtx_tmp.save();
|
|
|
|
previewCtx_tmp.translate(layerCX, layerCY);
|
|
|
|
previewCtx_tmp.scale(viewerScale, viewerScale);
|
|
|
|
previewCtx_tmp.translate((-doodleTransform[0]) * (globalScale * doodleTransform[2]), (-doodleTransform[1]) * (globalScale * doodleTransform[3]));
|
|
|
|
|
|
|
|
var adjustedDoodlePt = centeredAndScaledDoodlePoint(_points[0]);
|
|
|
|
|
|
|
|
previewCtx_tmp.beginPath();
|
|
|
|
previewCtx_tmp.moveTo(adjustedDoodlePt.x, adjustedDoodlePt.y);
|
|
|
|
for(var j = 1; j < _points.length; j++) {
|
|
|
|
adjustedDoodlePt = centeredAndScaledDoodlePoint(_points[j])
|
|
|
|
previewCtx_tmp.lineTo(adjustedDoodlePt.x, adjustedDoodlePt.y);
|
|
|
|
}
|
|
|
|
previewCtx_tmp.stroke();
|
|
|
|
previewCtx_tmp.closePath();
|
|
|
|
previewCtx_tmp.restore();
|
|
|
|
//*/
|
|
|
|
|
|
|
|
// var saved_rect = previewCtx_tmp.getImageData(0, 0, layerCX*2, layerCY*2);
|
|
|
|
var saved_rect_todataurl = preview_tmp.toDataURL();
|
|
|
|
doodleImageCapture = new Image();
|
|
|
|
doodleImageCapture.onload = function() {
|
|
|
|
|
|
|
|
previewCtx.clearRect(0, 0, preview.width, preview.height);
|
|
|
|
previewCtx.lineWidth = strokeWidth;
|
|
|
|
previewCtx.strokeStyle = '#f00'; //"rgba(255,255,0,0)";
|
|
|
|
|
|
|
|
var y = 0;
|
|
|
|
var r = 0;
|
|
|
|
|
|
|
|
for(var i=0;i<numLayers;i++) {
|
|
|
|
|
2013-09-18 22:35:38 +02:00
|
|
|
var verticalScaleFactor = scaleFunction(i / maxNumLayers);
|
|
|
|
|
2013-08-21 17:37:20 +02:00
|
|
|
if(i == 0 || i == Math.floor(numLayers/2) || i == numLayers-1){
|
|
|
|
previewCtx.globalAlpha = 1;
|
|
|
|
} else {
|
|
|
|
previewCtx.globalAlpha = globalAlpha;
|
|
|
|
}
|
|
|
|
|
|
|
|
previewCtx.save();
|
|
|
|
|
2013-10-11 13:42:12 +02:00
|
|
|
previewCtx.translate(layerCX,layerOffsetY+y);
|
2013-09-18 22:35:38 +02:00
|
|
|
// previewCtx.scale(1, scaleY)
|
|
|
|
previewCtx.scale(verticalScaleFactor, scaleY * verticalScaleFactor)
|
2013-08-21 17:37:20 +02:00
|
|
|
previewCtx.rotate(r);
|
|
|
|
previewCtx.translate(-layerCX,-layerCY);
|
|
|
|
|
|
|
|
previewCtx.drawImage(doodleImageCapture, 0, 0);
|
|
|
|
|
|
|
|
y -= yStep;
|
|
|
|
r += rStep;
|
|
|
|
previewCtx.restore();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
doodleImageCapture.src = saved_rect_todataurl;
|
|
|
|
|
|
|
|
previewCtx.globalAlpha = globalAlpha;
|
|
|
|
}
|
|
|
|
|
|
|
|
// called by the move up/down or twist left/right buttons
|
|
|
|
// it is assumed that the preview has been rendered to an Image object, which will be used to draw the preview with (much better performance)
|
2013-08-21 18:08:32 +02:00
|
|
|
function redrawRenderedPreview(redrawLess) {
|
|
|
|
if (redrawLess == undefined) redrawLess = false;
|
2013-10-29 22:08:08 +01:00
|
|
|
// console.log("f:redrawRenderedPreview()");
|
2013-08-21 17:37:20 +02:00
|
|
|
|
|
|
|
previewCtx.clearRect(0, 0, preview.width, preview.height);
|
|
|
|
previewCtx.lineWidth = strokeWidth;
|
|
|
|
previewCtx.strokeStyle = '#f00'; //"rgba(255,255,0,0)";
|
|
|
|
|
|
|
|
var y = 0;
|
|
|
|
var r = 0;
|
|
|
|
|
|
|
|
for(var i = 0; i < numLayers; i++) {
|
|
|
|
|
2013-09-18 22:35:38 +02:00
|
|
|
var verticalScaleFactor = scaleFunction(i / maxNumLayers);
|
|
|
|
|
2013-08-21 17:37:20 +02:00
|
|
|
if(i == 0 || i == Math.floor(numLayers/2) || i == numLayers-1){
|
|
|
|
previewCtx.globalAlpha = 1;
|
|
|
|
} else {
|
|
|
|
previewCtx.globalAlpha = globalAlpha;
|
|
|
|
}
|
|
|
|
|
2013-08-21 18:08:32 +02:00
|
|
|
if (redrawLess && i%2 != 0 && !(i == 0 || i == Math.floor(numLayers/2) || i == numLayers-1) ) {
|
|
|
|
y -= yStep;
|
|
|
|
r += rStep;
|
|
|
|
continue;
|
|
|
|
}
|
2013-08-21 17:37:20 +02:00
|
|
|
previewCtx.save();
|
|
|
|
|
2013-10-11 13:42:12 +02:00
|
|
|
previewCtx.translate(layerCX,layerOffsetY+y);
|
2013-09-18 22:35:38 +02:00
|
|
|
// previewCtx.scale(1, scaleY)
|
|
|
|
previewCtx.scale(verticalScaleFactor, scaleY * verticalScaleFactor);
|
2013-08-21 17:37:20 +02:00
|
|
|
previewCtx.rotate(r);
|
|
|
|
previewCtx.translate(-layerCX,-layerCY);
|
|
|
|
|
|
|
|
previewCtx.drawImage(doodleImageCapture, 0, 0);
|
|
|
|
|
|
|
|
y -= yStep;
|
|
|
|
r += rStep;
|
|
|
|
previewCtx.restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function centeredAndScaledDoodlePoint(p) {
|
|
|
|
var obj = { x: 0, y: 0};
|
|
|
|
|
|
|
|
obj.x = (p[0] - ((doodleBounds[2] - doodleBounds[0])/2)) * (globalScale * doodleTransform[2]);
|
|
|
|
obj.y = (p[1] - ((doodleBounds[3] - doodleBounds[1])/2)) * (globalScale * doodleTransform[3]);
|
|
|
|
// obj.x = (p[0] - (doodleBounds[2] - doodleBounds[0])) * (globalScale * doodleTransform[2]);
|
|
|
|
// obj.y = (p[1] - (doodleBounds[3] - doodleBounds[1])) * (globalScale * doodleTransform[3]);
|
|
|
|
// obj.x = (p[0] - doodleTransform[0]) * (globalScale * doodleTransform[2]);
|
|
|
|
// obj.y = (p[1] - doodleTransform[1]) * (globalScale * doodleTransform[3]);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
//*
|
|
|
|
var updatePrevX = -1;
|
|
|
|
var updatePrevY = -1;
|
|
|
|
function updatePreview(_x, _y, redrawLess) {
|
|
|
|
if (redrawLess == undefined) redrawLess = false;
|
|
|
|
redrawLess = false;
|
|
|
|
|
|
|
|
if (_points.length < 2) return;
|
|
|
|
if (updatePrevX == -1 || updatePrevY == -1) {
|
|
|
|
updatePrevX = _x;
|
|
|
|
updatePrevY = _y;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if (_points.length < 16 && Math.sqrt(Math.pow((updatePrevX - _x), 2) + Math.pow((updatePrevY - _y), 2)) < 8) return;
|
|
|
|
|
|
|
|
var y = 0;
|
|
|
|
var r = 0;
|
|
|
|
|
|
|
|
previewCtx.lineWidth = strokeWidth;
|
|
|
|
previewCtx.strokeStyle = '#f00'; //"rgba(255,255,0,0)";
|
|
|
|
|
|
|
|
for(var i = 0; i < numLayers; i++) {
|
|
|
|
|
|
|
|
if(i == 0 || i == Math.floor(numLayers/2) || i == numLayers-1) {
|
|
|
|
previewCtx.globalAlpha = 1;
|
|
|
|
} else {
|
|
|
|
previewCtx.globalAlpha = globalAlpha;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (redrawLess && i%debug_redrawSimplification != 0 && !(i == 0 || i == Math.floor(numLayers/2) || i == numLayers-1) ) {
|
|
|
|
y -= yStep;
|
|
|
|
r += rStep;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
previewCtx.save();
|
|
|
|
|
2013-10-11 13:42:12 +02:00
|
|
|
// previewCtx.translate(layerCX, layerOffsetY + layerCY + y);
|
|
|
|
previewCtx.translate(layerCX, layerOffsetY + y);
|
2013-08-21 17:37:20 +02:00
|
|
|
previewCtx.scale(viewerScale, scaleY * viewerScale);
|
|
|
|
previewCtx.rotate(r);
|
|
|
|
previewCtx.translate((-doodleTransform[0]) * (globalScale * doodleTransform[2]), (-doodleTransform[1]) * (globalScale * doodleTransform[3]));
|
|
|
|
|
|
|
|
|
|
|
|
previewCtx.beginPath();
|
|
|
|
var prevPoint = centeredAndScaledDoodlePoint([updatePrevX, updatePrevY]);
|
|
|
|
previewCtx.moveTo(prevPoint.x, prevPoint.y);
|
|
|
|
var adjustedDoodlePoint = centeredAndScaledDoodlePoint([_x, _y]);
|
|
|
|
previewCtx.lineTo(adjustedDoodlePoint.x, adjustedDoodlePoint.y);
|
|
|
|
previewCtx.stroke();
|
|
|
|
|
|
|
|
y -= yStep;
|
|
|
|
r += rStep;
|
|
|
|
previewCtx.restore();
|
|
|
|
}
|
|
|
|
previewCtx.globalAlpha = globalAlpha;
|
|
|
|
updatePrevX = _x;
|
|
|
|
updatePrevY = _y;
|
|
|
|
|
|
|
|
}
|
2013-12-11 22:21:05 +01:00
|
|
|
//*/
|