<commit_before>/**
 * ownCloud - Sketch
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright <NAME> 2015
 */

define(function(require) {
	'use strict';

	var Marionette = require('marionette');
	var _ = require('underscore');
	var $ = require('jquery');
	var Line = require('model/line');

	return Marionette.ItemView.extend({
		template: '#sketch-canvas-template',
		ui: {
			'canvas': '#sketch-canvas'
		},
		events: {
			'mousedown @ui.canvas': 'startLine',
			'mousemove @ui.canvas': 'drawLine',
			'mouseup @ui.canvas': 'finishLine',
			'mouseleave @ui.canvas': 'finishLine'
		},
		startLine: function(e) {
			e.stopPropagation();
			this.drawing = true;
			this.line = new Line();
			this.line.add(this.getCurrentMousePosition(e));
		},
		drawLine: function(e) {
			e.stopPropagation();
			if (this.drawing) {
				this.addLineToPath(e);
				this.draw();
			}
		},
		finishLine: function(e) {
			e.stopPropagation();
			if (this.drawing) {
				this.drawing = false;
				this.addLineToPath(e);
			}
		},
		initialize: function() {
			this.canvas = null;
			this.line = new Line();
			this.drawing = false;

			// Listen to window resize
			this.resizeHandler = _.bind(this.adjustCanvasSize, this);
			$(window).on('resize', this.resizeHandler);
		},
		onDestroy: function() {
			$(window).off('resize', this.resizeHandler);
		},
		onRender: function() {
			this.canvas = this.ui.canvas[0].getContext('2d');
			this.adjustCanvasSize();
		},
		adjustCanvasSize: function() {
			this.canvas.canvas.width = 1;
			this.canvas.canvas.height = 1;

			this.canvas.canvas.width = this.$el.innerWidth();
			this.canvas.canvas.height = this.$el.innerHeight();

			this.draw();
		},
		addLineToPath: function(e) {
			this.line.push(this.getCurrentMousePosition(e));
		},
		getCurrentMousePosition: function(e) {
			return {
				x: e.pageX - this.ui.canvas.offset().left,
				y: e.pageY - this.ui.canvas.offset().top
			};
		},
		draw: function() {
			var canvas = this.canvas;
			canvas.clearRect(0, 0, canvas.canvas.width, canvas.canvas.height);
			canvas.strokeStyle = "#1D2D44";
			canvas.lineJoin = "round";
			canvas.lineWidth = 1;

			var prev = null;
			this.line.each(function(point) {
				if (prev !== null) {
					canvas.beginPath();
					canvas.moveTo(prev.get('x'), prev.get('y'));
					canvas.lineTo(point.get('x'), point.get('y'));
					canvas.closePath();
					canvas.stroke();
				}
				prev = point;
			});
		}
	});
});<commit_msg>performance optimization: draw all paths at once<commit_after>/**
 * ownCloud - Sketch
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright <NAME> 2015
 */

define(function(require) {
	'use strict';

	var Marionette = require('marionette');
	var _ = require('underscore');
	var $ = require('jquery');
	var Line = require('model/line');

	return Marionette.ItemView.extend({
		template: '#sketch-canvas-template',
		ui: {
			'canvas': '#sketch-canvas'
		},
		events: {
			'mousedown @ui.canvas': 'startLine',
			'mousemove @ui.canvas': 'drawLine',
			'mouseup @ui.canvas': 'finishLine',
			'mouseleave @ui.canvas': 'finishLine'
		},
		startLine: function(e) {
			e.stopPropagation();
			this.drawing = true;
			this.line = new Line();
			this.line.add(this.getCurrentMousePosition(e));
		},
		drawLine: function(e) {
			e.stopPropagation();
			if (this.drawing) {
				this.addLineToPath(e);
				this.draw();
			}
		},
		finishLine: function(e) {
			e.stopPropagation();
			if (this.drawing) {
				this.drawing = false;
				this.addLineToPath(e);
			}
		},
		initialize: function() {
			this.canvas = null;
			this.line = new Line();
			this.drawing = false;

			// Listen to window resize
			this.resizeHandler = _.bind(this.adjustCanvasSize, this);
			$(window).on('resize', this.resizeHandler);
		},
		onDestroy: function() {
			$(window).off('resize', this.resizeHandler);
		},
		onRender: function() {
			this.canvas = this.ui.canvas[0].getContext('2d');
			this.adjustCanvasSize();
		},
		adjustCanvasSize: function() {
			this.canvas.canvas.width = 1;
			this.canvas.canvas.height = 1;

			this.canvas.canvas.width = this.$el.innerWidth();
			this.canvas.canvas.height = this.$el.innerHeight();

			this.draw();
		},
		addLineToPath: function(e) {
			this.line.push(this.getCurrentMousePosition(e));
		},
		getCurrentMousePosition: function(e) {
			return {
				x: e.pageX - this.ui.canvas.offset().left,
				y: e.pageY - this.ui.canvas.offset().top
			};
		},
		draw: function() {
			var canvas = this.canvas;
			canvas.clearRect(0, 0, canvas.canvas.width, canvas.canvas.height);
			canvas.strokeStyle = "#1D2D44";
			canvas.lineJoin = "round";
			canvas.lineWidth = 1;

			var prev = null;
			canvas.beginPath();
			this.line.each(function(point) {
				if (prev !== null) {
					canvas.moveTo(prev.get('x'), prev.get('y'));
					canvas.lineTo(point.get('x'), point.get('y'));
				}
				prev = point;
			});
			canvas.closePath();
			canvas.stroke();
		}
	});
});
{'fraction_non_alphanumeric': 0.1756202383277984, 'fraction_numerical': 0.006251220941590154, 'mean_word_length': 3.342663273960984, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3064641', 'n_tokens_mistral': 2095, 'n_tokens_neox': 1765, 'n_words': 336}