Открыть меню
Переключить меню настроек
Открыть персональное меню
Вы не представились системе
Ваш IP-адрес будет виден всем, если вы внесёте какие-либо изменения.

MediaWiki:Test.js: различия между версиями

Страница интерфейса MediaWiki
Нет описания правки
Нет описания правки
 
(не показано 208 промежуточных версий 2 участников)
Строка 1: Строка 1:
var canvas = document.createElement("canvas");
(function() {
canvas.width = 300;
    if (typeof mw === 'undefined' || !window.document) return;  
canvas.height = 300;
   
document.body.appendChild(canvas);
    function initCollapse() {
var ctx = canvas.getContext("2d");
        var headings = document.querySelectorAll('.chem-heading');  
 
        var hi = 0;  
var gridSize = 10;
        var len = headings.length;  
var snake = [{x: 50, y: 50}];
        for (; hi < len; hi++) {  
var direction = {x: gridSize, y: 0};
            (function(node) {  
var food = {x: 100, y: 100};
                if (node.getAttribute('data-chem-attached')) return;
var gameOver = false;
                node.setAttribute('data-chem-attached', '1');  
 
                node.style.cursor = 'pointer';  
function draw() {
                node.addEventListener('click', function() {  
    ctx.clearRect(0, 0, canvas.width, canvas.height);
                    var kind = node.getAttribute('data-kind') || '';  
 
                    var wrapper = findWrapper(node, kind);  
    ctx.fillStyle = "#00FF00";
                    if (!wrapper) return;
    snake.forEach(function(segment) {
                    var btn = node.querySelector('.collapse-btn');  
        ctx.fillRect(segment.x, segment.y, gridSize, gridSize);
                   
    });
                    if (wrapper.classList.contains('collapsed')) {  
 
                        wrapper.classList.remove('collapsed');  
    ctx.fillStyle = "#FF0000";
                        wrapper.classList.add('expanded');
    ctx.fillRect(food.x, food.y, gridSize, gridSize);
                        wrapper.style.maxHeight = wrapper.scrollHeight + 'px';  
 
                        if (btn) btn.textContent = 'свернуть';
    if (gameOver) {
                        var cleanup = function() {  
        ctx.fillStyle = "#000000";
                            wrapper.style.maxHeight = '';  
        ctx.fillText("Game Over!", canvas.width / 2 - 40, canvas.height / 2);
                            wrapper.removeEventListener('transitionend', cleanup);  
    }
                        };
}
                        wrapper.addEventListener('transitionend', cleanup);  
 
                    } else {
function update() {
                        var currentHeight = wrapper.scrollHeight;
    if (gameOver) return;
                        wrapper.style.maxHeight = currentHeight + 'px';
 
                        wrapper.offsetHeight;
    var newHead = {x: snake[0].x + direction.x, y: snake[0].y + direction.y};
                        wrapper.classList.remove('expanded');
 
                        wrapper.classList.add('collapsed');
    if (newHead.x < 0 || newHead.x >= canvas.width || newHead.y < 0 || newHead.y >= canvas.height || collision(newHead)) {
                        wrapper.style.maxHeight = '0px';
        gameOver = true;
                        if (btn) btn.textContent = 'развернуть';
        return;
                    }
    }
                });  
 
            })(headings[hi]);  
    snake.unshift(newHead);
        }  
 
    }  
    if (newHead.x === food.x && newHead.y === food.y) {
   
        food = {x: Math.floor(Math.random() * (canvas.width / gridSize)) * gridSize, y: Math.floor(Math.random() * (canvas.height / gridSize)) * gridSize};
    function findWrapper(node, kind) {  
    } else {
        var parent = node.parentNode;  
        snake.pop();
         if (!parent) return null;
    }
        var wrappers = parent.querySelectorAll('.collapsible');
}
        var wi = 0;  
 
         var wlen = wrappers.length;  
function collision(head) {
        for (; wi < wlen; wi++) {  
    for (var i = 1; i < snake.length; i++) {
            if (wrappers[wi].getAttribute('data-kind') === kind) {  
         if (head.x === snake[i].x && head.y === snake[i].y) {
                return wrappers[wi];  
            return true;
            }  
         }
         }  
    }
        return null;  
    return false;
     }  
}
   
 
    if (document.readyState === 'complete' || document.readyState === 'interactive') {  
function changeDirection(event) {
         initCollapse();  
    if (event.keyCode === 37 && direction.x === 0) {
     } else {  
        direction = {x: -gridSize, y: 0};
         document.addEventListener('DOMContentLoaded', initCollapse);  
    } else if (event.keyCode === 38 && direction.y === 0) {
     }  
         direction = {x: 0, y: -gridSize};
})();
     } else if (event.keyCode === 39 && direction.x === 0) {
         direction = {x: gridSize, y: 0};
     } else if (event.keyCode === 40 && direction.y === 0) {
         direction = {x: 0, y: gridSize};
    }
}
 
document.addEventListener("keydown", changeDirection);
 
function gameLoop() {
    update();
    draw();
    if (!gameOver) {
        setTimeout(gameLoop, 100);
     }
}
 
gameLoop();

Текущая версия от 13:10, 4 апреля 2026

(function() { 
    if (typeof mw === 'undefined' || !window.document) return; 
    
    function initCollapse() { 
        var headings = document.querySelectorAll('.chem-heading'); 
        var hi = 0; 
        var len = headings.length; 
        for (; hi < len; hi++) { 
            (function(node) { 
                if (node.getAttribute('data-chem-attached')) return; 
                node.setAttribute('data-chem-attached', '1'); 
                node.style.cursor = 'pointer'; 
                node.addEventListener('click', function() { 
                    var kind = node.getAttribute('data-kind') || ''; 
                    var wrapper = findWrapper(node, kind); 
                    if (!wrapper) return; 
                    var btn = node.querySelector('.collapse-btn'); 
                    
                    if (wrapper.classList.contains('collapsed')) { 
                        wrapper.classList.remove('collapsed'); 
                        wrapper.classList.add('expanded'); 
                        wrapper.style.maxHeight = wrapper.scrollHeight + 'px'; 
                        if (btn) btn.textContent = 'свернуть'; 
                        var cleanup = function() { 
                            wrapper.style.maxHeight = ''; 
                            wrapper.removeEventListener('transitionend', cleanup); 
                        }; 
                        wrapper.addEventListener('transitionend', cleanup); 
                    } else { 
                        var currentHeight = wrapper.scrollHeight; 
                        wrapper.style.maxHeight = currentHeight + 'px'; 
                        wrapper.offsetHeight; 
                        wrapper.classList.remove('expanded'); 
                        wrapper.classList.add('collapsed'); 
                        wrapper.style.maxHeight = '0px'; 
                        if (btn) btn.textContent = 'развернуть'; 
                    } 
                }); 
            })(headings[hi]); 
        } 
    } 
    
    function findWrapper(node, kind) { 
        var parent = node.parentNode; 
        if (!parent) return null; 
        var wrappers = parent.querySelectorAll('.collapsible'); 
        var wi = 0; 
        var wlen = wrappers.length; 
        for (; wi < wlen; wi++) { 
            if (wrappers[wi].getAttribute('data-kind') === kind) { 
                return wrappers[wi]; 
            } 
        } 
        return null; 
    } 
    
    if (document.readyState === 'complete' || document.readyState === 'interactive') { 
        initCollapse(); 
    } else { 
        document.addEventListener('DOMContentLoaded', initCollapse); 
    } 
})();