<commit_before>/*
	WebPlotDigitizer - http://arohatgi.info/WebPlotDigitizer

	Copyright 2010-2013 <NAME> <<EMAIL>>

	This file is part of WebPlotDigitizer.

    WebPlotDigitizer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    WebPlotDigitizer is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with WebPlotDigitizer.  If not, see <http://www.gnu.org/licenses/>.


*/



/* Zoomed-in view variables */
var zCanvas; 
var zctx;
var tempCanvas;
var tctx;
var zoom_dx = 20;
var zoom_dy = 20;
var zWindowWidth = 200;
var zWindowHeight = 200;
var mPosn;
var extendedCrosshair = false;
var pix = [];
pix[0] = new Array();

var zoomTimeout;

/**
 * Initialize Zoom Window
 */
function initZoom() {
	var zCrossHair = document.getElementById("zoomCrossHair");
	var zchCtx = zCrossHair.getContext("2d");
	zchCtx.strokeStyle = "rgb(0,0,0)";
	zchCtx.beginPath();
	zchCtx.moveTo(zWindowWidth/2, 0);
	zchCtx.lineTo(zWindowWidth/2, zWindowHeight);
	zchCtx.moveTo(0, zWindowHeight/2);
	zchCtx.lineTo(zWindowWidth, zWindowHeight/2);
	zchCtx.stroke();
	
}


/**
 *
 */
function updateZoomEventHandler(ev) {
	clearTimeout(zoomTimeout);
	zoomTimeout = setTimeout(updateZoom(ev), 5);
}

/**
 * Update view.
 */
function updateZoom(ev) {

	var posn = getPosition(ev);

	var xpos = posn.x;
	var ypos = posn.y;

	var dx = zoom_dx;
	var dy = zoom_dy;
    
    if (axesPicked != 1) {
        mPosn.innerHTML = xpos + ', ' + ypos;
    } else if(axesPicked == 1) {
        pix[0][0] = parseFloat(xpos);
        pix[0][1] = parseFloat(ypos);
        var rpix = pixelToData(pix, 1, plotType);
	
		if (plotType === 'image') {
		  mPosn.innerHTML = rpix[0][0] + ', ' + rpix[0][1];
		} else {
			if(plotType === 'XY') {
				if(axesAlignmentData[6] === true) {
					mPosn.innerHTML = dateConverter.formatDate(dateConverter.fromJD(rpix[0][0]), axesAlignmentData[8]);
				} else {
					mPosn.innerHTML = parseFloat(rpix[0][0]).toExponential(4);
				}

				if(axesAlignmentData[7] === true) {
					mPosn.innerHTML += ', ' + dateConverter.formatDate(dateConverter.fromJD(rpix[0][1]), axesAlignmentData[9]);
				} else {
					mPosn.innerHTML += ', ' + parseFloat(rpix[0][1]).toExponential(4);
				}
			} else {
				mPosn.innerHTML = parseFloat(rpix[0][0]).toExponential(4) + ', ' + parseFloat(rpix[0][1]).toExponential(4);
			}
			if (plotType === 'ternary') {
				mPosn.innerHTML += ', ' + parseFloat(rpix[0][2]).toExponential(4);
			}
		}
    }
    
  	if (extendedCrosshair === true) {
	    hoverCanvas.width = hoverCanvas.width;
	    hoverCtx.strokeStyle = "rgba(0,0,0, 0.5)";
	    hoverCtx.beginPath();
	    hoverCtx.moveTo(xpos, 0);
	    hoverCtx.lineTo(xpos, canvasHeight);
	    hoverCtx.moveTo(0, ypos);
	    hoverCtx.lineTo(canvasWidth, ypos);
	    hoverCtx.stroke();
	}

    
	if((xpos-dx/2) >= 0 && (ypos-dy/2) >= 0 && (xpos+dx/2) <= canvasWidth && (ypos+dy/2) <= canvasHeight) {
		var zoomImage = ctx.getImageData(xpos-dx/2,ypos-dy/2,dx,dy);
	    var dataLayerImage = dataCtx.getImageData(xpos-dx/2,ypos-dy/2,dx,dy);

        // merge data from the two layers.
        for (var zi = 0; zi < dataLayerImage.data.length; zi+=4) {
            if ((dataLayerImage.data[zi]+dataLayerImage.data[zi+1]+dataLayerImage.data[zi+2]+dataLayerImage.data[zi+3])!=0) {
                zoomImage.data[zi] = dataLayerImage.data[zi];
                zoomImage.data[zi+1] = dataLayerImage.data[zi+1];        
                zoomImage.data[zi+2] = dataLayerImage.data[zi+2];        
            }
        }
        
		tctx.putImageData(zoomImage,0,0);
		
		var imgdata = tempCanvas.toDataURL();
		var zImage = document.createElement('img');
		zImage.onload = function() { 
				zctx.drawImage(zImage,0,0,parseInt(zWindowWidth),parseInt(zWindowHeight)); 
			};
		zImage.src = imgdata;

	}
	
}

function toggleCrosshair(ev) {
    if (ev.keyCode === 220) {
        ev.preventDefault();
        extendedCrosshair = !(extendedCrosshair);
        hoverCanvas.width = hoverCanvas.width;
    }
    return;
}
<commit_msg>zoom view was leaking memory as every scaled image was cached. The canvas can now copy data from another canvas directly so creating an intermediate image is unnecessary. this should reduce memory usage by a lot<commit_after>/*
	WebPlotDigitizer - http://arohatgi.info/WebPlotDigitizer

	Copyright 2010-2013 <NAME> <<EMAIL>>

	This file is part of WebPlotDigitizer.

    WebPlotDigitizer is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    WebPlotDigitizer is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with WebPlotDigitizer.  If not, see <http://www.gnu.org/licenses/>.


*/



/* Zoomed-in view variables */
var zCanvas; 
var zctx;
var tempCanvas;
var tctx;
var zoom_dx = 20;
var zoom_dy = 20;
var zWindowWidth = 200;
var zWindowHeight = 200;
var mPosn;
var extendedCrosshair = false;
var pix = [];
pix[0] = new Array();

var zoomTimeout;

/**
 * Initialize Zoom Window
 */
function initZoom() {
	var zCrossHair = document.getElementById("zoomCrossHair");
	var zchCtx = zCrossHair.getContext("2d");
	zchCtx.strokeStyle = "rgb(0,0,0)";
	zchCtx.beginPath();
	zchCtx.moveTo(zWindowWidth/2, 0);
	zchCtx.lineTo(zWindowWidth/2, zWindowHeight);
	zchCtx.moveTo(0, zWindowHeight/2);
	zchCtx.lineTo(zWindowWidth, zWindowHeight/2);
	zchCtx.stroke();
	
}


/**
 *
 */
function updateZoomEventHandler(ev) {
	clearTimeout(zoomTimeout);
	zoomTimeout = setTimeout(updateZoom(ev), 5);
}

/**
 * Update view.
 */
function updateZoom(ev) {

	var posn = getPosition(ev);

	var xpos = posn.x;
	var ypos = posn.y;

	var dx = zoom_dx;
	var dy = zoom_dy;
    
    if (axesPicked != 1) {
        mPosn.innerHTML = xpos + ', ' + ypos;
    } else if(axesPicked == 1) {
        pix[0][0] = parseFloat(xpos);
        pix[0][1] = parseFloat(ypos);
        var rpix = pixelToData(pix, 1, plotType);
	
		if (plotType === 'image') {
		  mPosn.innerHTML = rpix[0][0] + ', ' + rpix[0][1];
		} else {
			if(plotType === 'XY') {
				if(axesAlignmentData[6] === true) {
					mPosn.innerHTML = dateConverter.formatDate(dateConverter.fromJD(rpix[0][0]), axesAlignmentData[8]);
				} else {
					mPosn.innerHTML = parseFloat(rpix[0][0]).toExponential(4);
				}

				if(axesAlignmentData[7] === true) {
					mPosn.innerHTML += ', ' + dateConverter.formatDate(dateConverter.fromJD(rpix[0][1]), axesAlignmentData[9]);
				} else {
					mPosn.innerHTML += ', ' + parseFloat(rpix[0][1]).toExponential(4);
				}
			} else {
				mPosn.innerHTML = parseFloat(rpix[0][0]).toExponential(4) + ', ' + parseFloat(rpix[0][1]).toExponential(4);
			}
			if (plotType === 'ternary') {
				mPosn.innerHTML += ', ' + parseFloat(rpix[0][2]).toExponential(4);
			}
		}
    }
    
  	if (extendedCrosshair === true) {
	    hoverCanvas.width = hoverCanvas.width;
	    hoverCtx.strokeStyle = "rgba(0,0,0, 0.5)";
	    hoverCtx.beginPath();
	    hoverCtx.moveTo(xpos, 0);
	    hoverCtx.lineTo(xpos, canvasHeight);
	    hoverCtx.moveTo(0, ypos);
	    hoverCtx.lineTo(canvasWidth, ypos);
	    hoverCtx.stroke();
	}

    
	if((xpos-dx/2) >= 0 && (ypos-dy/2) >= 0 && (xpos+dx/2) <= canvasWidth && (ypos+dy/2) <= canvasHeight) {
		var zoomImage = ctx.getImageData(xpos-dx/2,ypos-dy/2,dx,dy);
	    var dataLayerImage = dataCtx.getImageData(xpos-dx/2,ypos-dy/2,dx,dy);

        // merge data from the two layers.
        for (var zi = 0; zi < dataLayerImage.data.length; zi+=4) {
            if ((dataLayerImage.data[zi]+dataLayerImage.data[zi+1]+dataLayerImage.data[zi+2]+dataLayerImage.data[zi+3])!=0) {
                zoomImage.data[zi] = dataLayerImage.data[zi];
                zoomImage.data[zi+1] = dataLayerImage.data[zi+1];        
                zoomImage.data[zi+2] = dataLayerImage.data[zi+2];        
            }
        }
        
        tctx.clearRect(0,0,zoom_dx,zoom_dy);
		tctx.putImageData(zoomImage,0,0);

        // Draw directly from canvas. 
        // Creating a new image here caused a memory leak!
        zctx.drawImage(tempCanvas, 0, 0, zWindowWidth, zWindowHeight);
	}
}

function toggleCrosshair(ev) {
    if (ev.keyCode === 220) {
        ev.preventDefault();
        extendedCrosshair = !(extendedCrosshair);
        hoverCanvas.width = hoverCanvas.width;
    }
    return;
}

{'fraction_non_alphanumeric': 0.1424138313199601, 'fraction_numerical': 0.02216557685913776, 'mean_word_length': 3.2465882352941176, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2836238', 'n_tokens_mistral': 3422, 'n_tokens_neox': 3150, 'n_words': 683}