Con el conocimiento que hemos adquirido hasta ahora, crearemos un juego de rompecabezas. Para empezar, necesitaremos una rejilla donde acomodar las piezas del rompecabezas. Comencemos declarando el arreglo que contendrá dicha rejilla:
Al crear el rectángulo, para obtener su posición correcta, se han de multiplicar estas variables "x" y "y" por el ancho y el alto del rectángulo respectivamente, para que de esta forma, queden alineados correctamente dentro de la cuadrícula. Para darle un márgen a la cuadrícula, se le suman posteriormente la cantidad de pixeles que se le desea dar de cada lado. Así, en este caso, le daremos un margen de 25 pixeles en el eje "x" y de 100 pixeles en el eje "y":
Pasaremos ahora a dibujar la rejilla. En este caso, dibujaremos su contorno en lugar del relleno para dar la sensación de espacio vacío en cada espacio de la rejilla. También dibujaremos el valor numérico de cada espacio para asegurarnos que estos hayan sido creados en su lugar correspondiente:
Ahora que tenemos lista esta base, seguro te gustará agregar alguna forma de saber cuando todas las piezas han sido posicionadas en el lugar que les corresponde. Para ello, empezaremos agregando una nueva variable booleana que nos indicará si el juego ya ha concluido o no:
Finalmente, dibujamos un mensaje de felicitación en caso de que el juego haya concluido:
        grid = [],Al crear el rectángulo, para obtener su posición correcta, se han de multiplicar estas variables "x" y "y" por el ancho y el alto del rectángulo respectivamente, para que de esta forma, queden alineados correctamente dentro de la cuadrícula. Para darle un márgen a la cuadrícula, se le suman posteriormente la cantidad de pixeles que se le desea dar de cada lado. Así, en este caso, le daremos un margen de 25 pixeles en el eje "x" y de 100 pixeles en el eje "y":
        // Create grid and draggables
        var x = 0,
            y = 0;
        for (y = 0; y < 4; y += 1) {
            for (x = 0; x < 6; x += 1) {
                grid.push(new Rectangle2D(x * 25 + 25, y * 25 + 100, 25, 25, true));
                draggables.push(new Rectangle2D(random(canvas.width), random(canvas.height), 25, 25, false));
            }
        }Pasaremos ahora a dibujar la rejilla. En este caso, dibujaremos su contorno en lugar del relleno para dar la sensación de espacio vacío en cada espacio de la rejilla. También dibujaremos el valor numérico de cada espacio para asegurarnos que estos hayan sido creados en su lugar correspondiente:
        // Draw grid
        ctx.fillStyle = '#999';
        ctx.strokeStyle = '#999';
        for (i = 0, l = grid.length; i < l; i += 1) {
            grid[i].stroke(ctx);
            ctx.fillText(i, grid[i].x, grid[i].y);
        }        // Draw rectangles
        for (i = draggables.length - 1; i > -1; i -= 1) {
            ctx.fillStyle = '#00f';
            draggables[i].fill(ctx);
            ctx.fillStyle = '#fff';
            ctx.fillText(i, draggables[i].x, draggables[i].y);
        }                // Snap draggable intro grid
                if (grid[dragging].contains(pointer)) {
                    draggables[dragging].x = grid[dragging].x;
                    draggables[dragging].y = grid[dragging].y;
                }Ahora que tenemos lista esta base, seguro te gustará agregar alguna forma de saber cuando todas las piezas han sido posicionadas en el lugar que les corresponde. Para ello, empezaremos agregando una nueva variable booleana que nos indicará si el juego ya ha concluido o no:
        isFinished = false,                isFinished = getPuzzleSolved();    function getPuzzleSolved() {
        for (i = 0, l = grid.length; i < l; i += 1) {
            if (grid[i].x !== draggables[i].x || grid[i].y !== draggables[i].y) {
                return false;
            }
        }
        return true;
    }Finalmente, dibujamos un mensaje de felicitación en caso de que el juego haya concluido:
        // Is the game finished?
        ctx.fillStyle = '#fff';
        if (isFinished) {
            ctx.fillText('Well done!', canvas.width / 2, canvas.height / 2);
        }Código final:
/*jslint bitwise: true, es5: true */
(function (window, undefined) {
    'use strict';
    var canvas = null,
        ctx = null,
        lastPress = null,
        lastRelease = null,
        mouse = {x: 0, y: 0},
        pointer = {x: 0, y: 0},
        dragging = null,
        draggables = [],
        grid = [],
        isFinished = false,
        i = 0,
        l = 0;
    
    function Rectangle2D(x, y, width, height, createFromTopLeft) {
        this.width = (width === undefined) ? 0 : width;
        this.height = (height === undefined) ? this.width : height;
        if (createFromTopLeft) {
            this.left = (x === undefined) ? 0 : x;
            this.top = (y === undefined) ? 0 : y;
        } else {
            this.x = (x === undefined) ? 0 : x;
            this.y = (y === undefined) ? 0 : y;
        }
    }
    
    Rectangle2D.prototype = {
        left: 0,
        top: 0,
        width: 0,
        height: 0,
        
        get x() {
            return this.left + this.width / 2;
        },
        set x(value) {
            this.left = value - this.width / 2;
        },
        
        get y() {
            return this.top + this.height / 2;
        },
        set y(value) {
            this.top = value - this.height / 2;
        },
        
        get right() {
            return this.left + this.width;
        },
        set right(value) {
            this.left = value - this.width;
        },
        
        get bottom() {
            return this.top + this.height;
        },
        set bottom(value) {
            this.top = value - this.height;
        },
        
        contains: function (rect) {
            if (rect !== undefined) {
                return (this.left < (rect.left || rect.x) &&
                    this.right > (rect.right || rect.x) &&
                    this.top < (rect.top || rect.y) &&
                    this.bottom > (rect.bottom || rect.y));
            }
        },
        
        intersects: function (rect) {
            if (rect !== undefined) {
                return (this.left < rect.right &&
                    this.right > rect.left &&
                    this.top < rect.bottom &&
                    this.bottom > rect.top);
            }
        },
        
        fill: function (ctx) {
            if (ctx !== undefined) {
                ctx.fillRect(this.left, this.top, this.width, this.height);
            }
        },
        
        stroke: function (ctx) {
            if (ctx !== undefined) {
                ctx.strokeRect(this.left, this.top, this.width, this.height);
            }
        }
    };
    
    function enableInputs() {
        document.addEventListener('mousemove', function (evt) {
            mouse.x = evt.pageX - canvas.offsetLeft;
            mouse.y = evt.pageY - canvas.offsetTop;
        }, false);
        
        document.addEventListener('mouseup', function (evt) {
            lastRelease = evt.which;
        }, false);
        
        canvas.addEventListener('mousedown', function (evt) {
            evt.preventDefault();
            lastPress = evt.which;
        }, false);
        
        canvas.addEventListener('touchmove', function (evt) {
            evt.preventDefault();
            var t = evt.targetTouches;
            mouse.x = t[0].pageX - canvas.offsetLeft;
            mouse.y = t[0].pageY - canvas.offsetTop;
        }, false);
        
        canvas.addEventListener('touchstart', function (evt) {
            evt.preventDefault();
            lastPress = 1;
            var t = evt.targetTouches;
            mouse.x = t[0].pageX - canvas.offsetLeft;
            mouse.y = t[0].pageY - canvas.offsetTop;
        }, false);
        
        canvas.addEventListener('touchend', function (evt) {
            lastRelease = 1;
        }, false);
        
        canvas.addEventListener('touchcancel', function (evt) {
            lastRelease = 1;
        }, false);
    }
    
    function random(max) {
        return ~~(Math.random() * max);
    }
    
    function getPuzzleSolved() {
        for (i = 0, l = grid.length; i < l; i += 1) {
            if (grid[i].x !== draggables[i].x || grid[i].y !== draggables[i].y) {
                return false;
            }
        }
        return true;
    }
    function paint(ctx) {
        // Clean canvas
        ctx.fillStyle = '#ccf';
        ctx.fillRect(0, 0, canvas.width, canvas.height);
        
        // Set default text properties
        ctx.textAlign = 'center';
        
        // Draw grid
        ctx.fillStyle = '#999';
        ctx.strokeStyle = '#999';
        for (i = 0, l = grid.length; i < l; i += 1) {
            grid[i].stroke(ctx);
            ctx.fillText(i, grid[i].x, grid[i].y);
        }
        
        // Draw rectangles
        for (i = draggables.length - 1; i > -1; i -= 1) {
            ctx.fillStyle = '#00f';
            draggables[i].fill(ctx);
            ctx.fillStyle = '#fff';
            ctx.fillText(i, draggables[i].x, draggables[i].y);
        }
        
        // Debug pointer position
        ctx.fillStyle = '#0f0';
        ctx.fillRect(pointer.x - 1, pointer.y - 1, 2, 2);
        
        // Is the game finished?
        ctx.fillStyle = '#fff';
        if (isFinished) {
            ctx.fillText('Well done!', canvas.width / 2, canvas.height / 2);
        }
        
        // Debug dragging rectangle
        //ctx.fillText('Dragging: ' + dragging, 0, 10);
    }
        
    function act() {
        // Set pointer to mouse
        pointer.x = mouse.x;
        pointer.y = mouse.y;
        
        // Limit pointer into canvas
        if (pointer.x < 0) {
            pointer.x = 0;
        }
        if (pointer.x > canvas.width) {
            pointer.x = canvas.width;
        }
        if (pointer.y < 0) {
            pointer.y = 0;
        }
        if (pointer.y > canvas.height) {
            pointer.y = canvas.height;
        }
        
        if (lastPress === 1) {
            // Check for current dragging rectangle
            for (i = 0, l = draggables.length; i < l; i += 1) {
                if (draggables[i].contains(pointer)) {
                    dragging = i;
                    isFinished = false;
                    break;
                }
            }
        }
        
        if (dragging !== null) {
            // Move current dragging rectangle
            draggables[dragging].x = pointer.x;
            draggables[dragging].y = pointer.y;
            
            if (lastRelease === 1) {
                // Snap draggable intro grid
                if (grid[dragging].contains(pointer)) {
                    draggables[dragging].x = grid[dragging].x;
                    draggables[dragging].y = grid[dragging].y;
                    isFinished = getPuzzleSolved();
                }
                // Release current dragging rectangle
                dragging = null;
            }
        }
    }
    function run() {
        window.requestAnimationFrame(run);
        act();
        paint(ctx);
        
        lastPress = null;
        lastRelease = null;
    }
    
    function init() {
        // Get canvas and context
        canvas = document.getElementById('canvas');
        ctx = canvas.getContext('2d');
        canvas.width = 200;
        canvas.height = 300;
        
        // Create grid and draggables
        var x = 0,
            y = 0;
        for (y = 0; y < 4; y += 1) {
            for (x = 0; x < 6; x += 1) {
                grid.push(new Rectangle2D(x * 25 + 25, y * 25 + 100, 25, 25, true));
                draggables.push(new Rectangle2D(random(canvas.width), random(canvas.height), 25, 25, false));
            }
        }
        
        // Start game
        enableInputs();
        run();
    }
    
    window.addEventListener('load', init, false);
}(window));
 
As reported by Stanford Medical, It is indeed the ONLY reason this country's women get to live 10 years more and weigh 19 KG lighter than us.
ResponderBorrar(And really, it is not about genetics or some hard exercise and really, EVERYTHING related to "how" they are eating.)
BTW, I said "HOW", not "WHAT"...
TAP this link to reveal if this short quiz can help you discover your real weight loss potential