{"size": 7593, "ext": "js", "max_stars_repo_path": "public/js/drawer.js", "max_stars_repo_name": "lss233/100bits", "max_stars_repo_stars_event_min_datetime": "2019-01-01T06:41:04.000Z", "max_stars_repo_stars_event_max_datetime": "2021-09-25T11:58:34.000Z", "max_issues_repo_path": "public/js/drawer.js", "max_issues_repo_name": "lss233/100bits", "max_issues_repo_issues_event_min_datetime": "2019-07-22T11:20:06.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-26T14:41:27.000Z", "max_forks_repo_path": "public/js/drawer.js", "max_forks_repo_name": "lss233/100bits", "max_forks_repo_forks_event_min_datetime": "2019-01-01T12:55:58.000Z", "max_forks_repo_forks_event_max_datetime": "2021-09-25T11:58:35.000Z"}
{"max_stars_count": 9, "max_issues_count": 10, "max_forks_count": 3, "avg_line_length": 30.7408906883, "max_line_length": 121, "alphanum_fraction": 0.5199525879}
var DECLARE = "别看了，乱七八糟的 浪费时间。"
eval("'"+DECLARE + "'")
var canvas = document.getElementById('drawer');
var ctx = canvas.getContext('2d');
var ratio = 2
drewPoints = []
if(!ga){
    function ga(a, b, c, d, e){
        return;
    }
}
function record(x, y){
    x = Number.parseInt(x / ratio) * ratio
    y = Number.parseInt(y / ratio) * ratio
    for (let i = 0; i < drewPoints.length; i++) {
        const element = drewPoints[i];
        if(element[0] == x && element[1] == y){
            drewPoints.splice(i,1)
            return
        }
    }
    drewPoints.push([x, y])
    $('[data-label=countLeft').text(countLeft - drewPoints.length)
}
function draw(x, y){
    x = Number.parseInt(x / ratio) * ratio
    y = Number.parseInt(y / ratio) * ratio
    var imgd = ctx.getImageData(x, y, 1, 1).data
    var p = imgd[1] == 0
    ctx.fillStyle = p ? '#fff': '#000'
    ctx.fillRect(x,y,ratio,ratio);
}
if(!window.login){
    var askLogin = function (e){
        alert('请先登录~')
        ga('send', 'event', '100Bits!', 'interact', 'notLoggedIn');
        return
    }
    canvas.onmouseup = askLogin
    canvas.ontouchend = askLogin
}
if (document.body.ontouchstart !== undefined) {
    // Mobile
    canvas.ontouchstart = function (e) {
        var x = e.touches[0].pageX - canvas.offsetLeft
        var y = e.touches[0].pageY - canvas.offsetTop
        if(countLeft - drewPoints.length > 0){
            draw(x,y)
            record(x,y)
            ga('send', 'event', '100Bits!', 'drawTouch', 'x:' + x + ',y: ' + y);
        }
    }
} else {
    canvas.onmousedown = function(e) {
        var x = e.pageX  - canvas.offsetLeft
        var y = e.pageY - canvas.offsetTop
        if(countLeft - drewPoints.length > 0){
            draw(x,y)
            record(x,y)
            ga('send', 'event', '100Bits!', 'drawMouse', 'x:' + x + ',y: ' + y);
        }
    }
}


$(document).ready(function() {
    window.countLeft = 100
    var ACTIVED_CSS = 'uk-button-danger'
    var DEFAULT_SPEED = 50
    var progressbar = $('#progressbar')
    var playProgress = undefined
    var last = []
    var isReserve = false
    var playBtn = $('.btn-play')
    var nextBtn = $('.btn-next')
    var prevBtn = $('.btn-prev')
    var towardBtn = $('.btn-toward')
    var backwardBtn = $('.btn-backward')
    var resetBtn = $('.btn-reset')
    var uploadBtn = $('.btn-upload')
    var immediatelyRender = (pos, checkReserve=false) => {
        return function (){
            var point = last[pos]
            $('[data-label=current-pos]').text(pos)
            $('[data-label=total-pos]').text(last.length)
            if(point != undefined){
                draw(point[0], point[1])
                $('[data-label=user]').text(point[2])
                if(checkReserve)
                    if(isReserve){
                        progressbar.val(Number.parseInt(pos) - 1)
                    } else {
                        progressbar.val(Number.parseInt(pos) + 1)
                    }
            }
        }
    }
    var performDrew = () => {
        enableBtn(playBtn)
        setImmediate(immediatelyRender(progressbar.val(), true))
        if(progressbar.val() == last.length && !isReserve){
            clearInterval(playProgress)
            inactiveBtn(playBtn)
            disableBtn(playBtn)
            disableBtn(nextBtn)
            disableBtn(towardBtn)
            enableBtn(prevBtn)
            enableBtn(backwardBtn)
        } else if(progressbar.val() == 0 && isReserve){
            clearInterval(playProgress)
            inactiveBtn(playBtn)
            disableBtn(playBtn)
            disableBtn(prevBtn)
            disableBtn(backwardBtn)
            enableBtn(nextBtn)
            enableBtn(towardBtn)
        } else if(progressbar.val() > 0 && progressbar.val() < last.length){
            enableBtn(nextBtn)
            enableBtn(towardBtn)
        }
    }
    var isBtnActived = (btn) => {
        return btn.hasClass(ACTIVED_CSS)
    }
    var inactiveBtn = (btn) => {
        return btn.removeClass(ACTIVED_CSS)
    }
    var activeBtn = (btn) => {
        return btn.addClass(ACTIVED_CSS)
    }
    var disableBtn = (btn) => {
        return btn.attr('disabled', true)
    }
    var enableBtn = (btn) => {
        return btn.attr('disabled', false)
    }
    var serialDraw = (pos) => {
        clearInterval(playProgress)
        inactiveBtn(playBtn)
        var current = Number.parseInt($('[data-label=current-pos]').text())
        for(; current != pos;){
            setImmediate(immediatelyRender(current))
            if(pos > current)
                current ++
            else if(pos < current)
                current --
        }
    }

    $.get('api/pic')
     .then(res =>{
        res.forEach(e => {
            last.push([e.x, e.y, e.user])
        });
        progressbar.attr('max', last.length)
        playBtn.click()
        return $.get('count')
    }).then(res => {
        countLeft = 100 - Number.parseInt(res)
        $('[data-label=countLeft').text(countLeft - drewPoints.length)
    })

    $.get('api/plate')
      .then(res => {
        var plate = new Image()
        plate.src = res.url
        ctx.drawImage(plate, 0, 0)
    })
    $('canvas').click(() => {
        if(Number.parseInt($('[data-label=current-pos]').text()) != Number.parseInt($('[data-label=total-pos]').text())){
            serialDraw(last.length + 1)
            progressbar.val(last.length)
        }
    })
    progressbar.change(() => {
        serialDraw(Number.parseInt(progressbar.val()))
    })
    nextBtn.click(performDrew)
    prevBtn.click(() => {
        isReserve = true
        performDrew()
        isReserve = false
    })
    towardBtn.click(() => {
        if(isBtnActived(towardBtn)){
            clearInterval(playProgress)
            playProgress = setInterval(performDrew, 2);
        } else {
            isReserve = false
            inactiveBtn(backwardBtn)
            activeBtn(towardBtn)
            clearInterval(playProgress)
            playProgress = setInterval(performDrew, DEFAULT_SPEED);
        }
    })
    backwardBtn.click(() => {
        if(isBtnActived(backwardBtn)){
            clearInterval(playProgress)
            playProgress = setInterval(performDrew, 2);
        } else {
            isReserve = true
            inactiveBtn(towardBtn)
            activeBtn(backwardBtn)
            if(progressbar.val() > 0 && isReserve){
                activeBtn(playBtn)
                enableBtn(playBtn)
                enableBtn(prevBtn)
                enableBtn(backwardBtn)
                if(progressbar.val() < last.length)
                    enableBtn(nextBtn)
                return
            }
            clearInterval(playProgress)
            playProgress = setInterval(performDrew, DEFAULT_SPEED);
        }
    })
    playBtn.click(function(){
        if(isBtnActived(playBtn)){
            clearInterval(playProgress)
            inactiveBtn(playBtn)
        } else {
            activeBtn(playBtn)
            playProgress = setInterval(performDrew, DEFAULT_SPEED);
        }
    })
    resetBtn.click(function(){
        var a = drewPoints
        a.forEach(e => {
            draw(e[0], e[1])
            record(e[0], e[1])
        });
    })
    uploadBtn.click(function(){
        UIkit.notification('正在上传...');
        $.post('upload', {
            points: drewPoints,
            _token:$("input[name='_token']").val()
        })
         .then(res => {
            UIkit.notification('上传成功!');
         })
         .catch(err => {
            UIkit.notification('上传失败! 太多bug了..');
         })
    })
});
