Código de JavaScript - Animación canvas Montañas con estrellas

Requerimientos

Navegador web que soporte canvas y versión 5 de html

https://www.hmlearning.com/

1.0
estrellaestrellaestrellaestrellaestrella(2)

Publicado el 13 de Agosto del 2018gráfica de visualizaciones de la versión: 1.0
2.653 visualizaciones desde el 13 de Agosto del 2018
estrellaestrellaestrellaestrellaestrella
estrellaestrellaestrellaestrella
estrellaestrellaestrella
estrellaestrella
estrella

Aqui podemos ver la imagen de la animación
fondo-estrellado
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<html>
<head>
    <title></title>
    <style>
        *{margin:0;padding: 0}
        body{background: black;}
    </style>
</head>
<body>
<canvas id="lienzo" width="1280" height="720">Tu navegador no soporta la api de canvas, asegurate de que tu navegador este actualizado o que soporte html5.</canvas>
<script>
    var c ,cx,centerX,centerY;
    var estrellas = [];
    var rotacion =0;
    window.addEventListener("load", init);
    function init() {
        c = document.getElementById("lienzo");
        cx = c.getContext("2d");
        fullScreen(c);
        //Establecemos las posiciones de 20 puntos en un array
        for (var u = 0; u < 50; u++){
            estrellas.push({
                x: centerX = Math.floor(Math.round(Math.random() * (-c.width/2 - c.width/2) + c.width/2)),
                y: centerY = Math.floor(Math.round(Math.random() * (-c.width/2 - c.width/2) + c.width/2))
            });
        }
        actualizar();
    }
    function fullScreen(c){
        var X, Y, wiw, wih, wow, woh,scale,cw,ch;
        X = window.innerWidth/2 - c.offsetWidth/2;
        Y = window.innerHeight/2 - c.offsetHeight/2;
 
        //alto y ancho de la ventana (lo que se puede ver)
        wiw = window.innerWidth;
        wih = window.innerHeight;
 
        //ancho y alto de la ventana (lo que se puede ver + lo de el scroll)
        wow = window.outerWidth;
        woh = window.outerHeight;
 
        //alto y ancho del canvas (lo que se puede ver en pantalla)
        cw = c.offsetWidth;
        ch = c.offsetHeight;
 
        scale = 1.77; //relacion aspecto 16:9
        c.style.transform = 'translate('+ X +'px,'+ Y +'px)' ;
 
        // Hacer bien el condicional
        if(wiw > cw || wih === ch && wiw > (wih * scale)){//si la ventana tiene mas ancho del canvaso igual
            c.style.width = window.innerHeight * scale;
            c.style.height = window.innerHeight;
        }else if(wih > c.offsetHeight){
            c.style.display = "block";
            c.style.margin = "auto";
            c.style.width = window.innerWidth;
            c.style.height = window.innerHeight * - scale;
        }
        //Se detecta si la ventana ha sido redimensionada y se vuelve a escalar
        window.onresize = function () {
            fullScreen(c);
        }
    }
    function actualizar(){
       window.requestAnimationFrame(actualizar);
        var degFondo = cx.createLinearGradient(0,500,0,0);
        degFondo.addColorStop(1,"#003072");
        degFondo.addColorStop(0,"#0053c6");
        //Dibjuamos el fondo
        cx.beginPath();
        cx.rect(0,0,c.width,c.height);
        cx.fillStyle = degFondo;
        cx.fill();
 
        rotacion--;
        console.log(rotacion);
        cx.translate(c.width/2, c.height/2);
        cx.rotate((rotacion > 160)?rotacion=0:rotacion * Math.PI / 180);
        for (var i = 0; i < 50; i++) {
            cx.beginPath();
            cx.arc(estrellas[i].x, estrellas[i].y, 5, 0, 2 * Math.PI);
            cx.fillStyle = "#fff"; // color de las estrellas
            cx.strokeStyle = "#d5d5d5";
            cx.fill();
            cx.stroke();
        }
        //Dibujamos los puntos o estrellas con el array de posiciones anterior
        cx.resetTransform();
        //Dibujamos la luna
        cx.beginPath();
        cx.fillStyle="#bfd3cc";
        cx.arc(c.width/18,c.height/8,120,0,6.3);
        cx.fill();
        //Dibjuamos la montaña de fondo
        cx.beginPath();
        cx.fillStyle="#3778b5";
        cx.moveTo(80,c.height);
        cx.lineTo(c.width/2,100);
        cx.lineTo(c.width-80,c.height);
        cx.fill();
 
        cx.beginPath();
        cx.fillStyle="#1074b5";
        cx.moveTo(0,c.height);
        cx.lineTo(c.width/4,c.height/3);
        cx.lineTo(c.width/2,c.height);
        cx.fill();
 
        cx.beginPath();
        cx.fillStyle="#1a6db5";
        cx.moveTo(c.width/2,c.height);
        cx.lineTo(c.width/1.3,c.height/3);
        cx.lineTo(c.width,c.height);
        cx.fill();
 
        cx.beginPath();
        cx.fillStyle="#0769a5";
        cx.moveTo(0,c.height);
        cx.lineTo(c.width/2,c.height/1.5);
        cx.lineTo(c.width,c.height);
        cx.fill();
 
    }
 
 
</script>
</body>
</html>



Comentarios sobre la versión: 1.0 (2)

Imágen de perfil
13 de Agosto del 2018
estrellaestrellaestrellaestrellaestrella
Muy bueno!!!
Responder
Juan Padilla
13 de Agosto del 2018
estrellaestrellaestrellaestrellaestrella
Espectacular
Responder

Comentar la versión: 1.0

Nombre
Correo (no se visualiza en la web)
Valoración
Comentarios...
CerrarCerrar
CerrarCerrar
Cerrar

Tienes que ser un usuario registrado para poder insertar imágenes, archivos y/o videos.

Puedes registrarte o validarte desde aquí.

Codigo
Negrita
Subrayado
Tachado
Cursiva
Insertar enlace
Imagen externa
Emoticon
Tabular
Centrar
Titulo
Linea
Disminuir
Aumentar
Vista preliminar
sonreir
dientes
lengua
guiño
enfadado
confundido
llorar
avergonzado
sorprendido
triste
sol
estrella
jarra
camara
taza de cafe
email
beso
bombilla
amor
mal
bien
Es necesario revisar y aceptar las políticas de privacidad

http://lwp-l.com/s4751