<commit_before>/*global Javelin:true */

'use strict';

/**
 * Canvas2d draws sprite components for 2d scenes.
 */
Javelin.Plugin.Canvas2d = function(plugin, config) {
    plugin.config = config;
    
    plugin.$onLoad = function() {
        plugin.fps = plugin.config.framesPerSecond || plugin.$engine.stepsPerSecond;
        plugin.lastTimeRendered = 0.0;        
        var target = document.getElementById(plugin.config.renderTargetId);
        if (!target) {
            throw new Error("No render target defined!");
        }
        var canvas = document.createElement('canvas');
        plugin.canvas = canvas;
        plugin.context = canvas.getContext('2d');
        plugin.canvas.height = plugin.config.height;
        plugin.canvas.width = plugin.config.width;
        target.appendChild(canvas);
        
        //TODO: implement layers, create canvas(es) internally
    };
    
    plugin.$onGameObjectDestroy = function(go) {
        
    };
    
    plugin.$onGameObjectCreate = function(go) {
        var sprite = go.getComponent('sprite');
        if (sprite) {
            sprite.visible = true;
        }
    };
    
    plugin.$onStep = function(deltaTime) {
        
        if (plugin.$engine.time - plugin.lastTimeRendered >= plugin.fps && !plugin.$engine.isRunningSlowly) {
            var ctx = plugin.context;
            var canvas = plugin.canvas;

            //clear the canvas
            ctx.clearRect(0, 0, canvas.width, canvas.height);
        
            //loop over gos and draw any sprite components
            var gos = plugin.$engine.gos;
            var l = gos.length;
            for (var i = 0; i < l; i++) {
                var s = gos[i].getComponent('sprite');
                if (s && s.visible && s.image) {
                    var t = gos[i].getComponent('transform2d');
                    var pos = t.position;
                    var rot = t.getWorldRotation();
                    var scale = s.scale;

                    //TODO: implement rotation (http://stackoverflow.com/questions/3793397/html5-canvas-drawimage-with-at-an-angle)

                    if (s.image instanceof Javelin.Asset.AtlasImage) {
                        var spr = s.image;
                        ctx.drawImage(spr.image, spr.x, spr.y, spr.width, spr.height, pos.x + spr.cx, pos.y + spr.cy, spr.width * scale.x, spr.height * scale.y);
                    } else {
                        var h = s.image.height * s.scale.y;
                        var w = s.image.width * s.scale.x;
                        ctx.drawImage(s.image, pos.x, pos.y, w, h);
                    }
                }
                
                var cbs = gos[i].getCallbacks('canvas2d.draw');
                for (var j in cbs) {
                    cbs[j](ctx);
                }
            }
            
            plugin.lastTimeRendered = plugin.$engine.time;
        }
    };
};
Javelin.Plugin.Canvas2d.alias = "canvas2d";<commit_msg>started on canvas sprite rotation, but it's not right yet<commit_after>/*global Javelin:true */

'use strict';

/**
 * Canvas2d draws sprite components for 2d scenes.
 */
Javelin.Plugin.Canvas2d = function(plugin, config) {
    plugin.config = config;
    
    plugin.$onLoad = function() {
        plugin.fps = plugin.config.framesPerSecond || plugin.$engine.stepsPerSecond;
        plugin.lastTimeRendered = 0.0;        
        var target = document.getElementById(plugin.config.renderTargetId);
        if (!target) {
            throw new Error("No render target defined!");
        }
        var canvas = document.createElement('canvas');
        plugin.canvas = canvas;
        plugin.context = canvas.getContext('2d');
        plugin.canvas.height = plugin.config.height;
        plugin.canvas.width = plugin.config.width;
        target.appendChild(canvas);
        
        //TODO: implement layers, create canvas(es) internally
    };
    
    plugin.$onGameObjectDestroy = function(go) {
        
    };
    
    plugin.$onGameObjectCreate = function(go) {
        var sprite = go.getComponent('sprite');
        if (sprite) {
            sprite.visible = true;
        }
    };
    
    plugin.$onStep = function(deltaTime) {
        
        if (plugin.$engine.time - plugin.lastTimeRendered >= plugin.fps && !plugin.$engine.isRunningSlowly) {
            var ctx = plugin.context;
            var canvas = plugin.canvas;

            //clear the canvas
            ctx.clearRect(0, 0, canvas.width, canvas.height);
        
            //loop over gos and draw any sprite components, and execute any `canvas2d.draw` callbacks
            var gos = plugin.$engine.gos;
            var l = gos.length;
            for (var i = 0; i < l; i++) {
                ctx.save();
                var s = gos[i].getComponent('sprite');
                if (s && s.visible && s.image) {
                    var t = gos[i].getComponent('transform2d');
                    var pos = t.position;
                    var rot = t.getWorldRotation();
                    var scale = s.scale;

                    //TODO: implement rotation (http://stackoverflow.com/questions/3793397/html5-canvas-drawimage-with-at-an-angle)

                    if (s.image instanceof Javelin.Asset.AtlasImage) {
                        var spr = s.image;

                        ctx.translate(pos.x + spr.cx, pos.y + spr.cy);
                        
                        //convert degrees to radians
                        ctx.rotate(rot * Math.PI/180);

                        ctx.drawImage(spr.image, spr.x, spr.y, spr.width, spr.height, 0, 0, spr.width * scale.x, spr.height * scale.y);
                    } else {
                        //TODO: rotation here as well
                        var h = s.image.height * s.scale.y;
                        var w = s.image.width * s.scale.x;
                        ctx.drawImage(s.image, pos.x, pos.y, w, h);
                    }
                    
                }
                
                var cbs = gos[i].getCallbacks('canvas2d.draw');
                for (var j in cbs) {
                    cbs[j](ctx);
                }

                ctx.restore();
            }
            
            plugin.lastTimeRendered = plugin.$engine.time;
        }
    };
};
Javelin.Plugin.Canvas2d.alias = "canvas2d";
{'fraction_non_alphanumeric': 0.11827105763141228, 'fraction_numerical': 0.007599746675110829, 'mean_word_length': 1.4532038834951457, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2207774', 'n_tokens_mistral': 1819, 'n_tokens_neox': 1728, 'n_words': 376}