LoginSignup
1
0

More than 3 years have passed since last update.

blocklyでobjectを動かす。 その7

Posted at

概要

blocklyでobjectを動かして見る。
sin波、やってみた。

写真

image.png

ステージを作る

<canvas id="canvas" width="400" height="300"></canvas>

ブロックを書く。

Blockly.Blocks.plot = {
    init: function() {
        this.jsonInit({
        message0: "plot %1",
        args0: [{
            type: "input_value",
            name: "TEXT"
        }],
        previousStatement: null,
        nextStatement: null,
        style: "text_blocks",
        tooltip: Blockly.Msg.TEXT_PRINT_TOOLTIP,
        helpUrl: Blockly.Msg.TEXT_PRINT_HELPURL
    })
}};

フローを書く。

image.png

objectを書く。

Blockly.JavaScript.plot = function(a) {
    return "plot(" + (Blockly.JavaScript.valueToCode(a, "TEXT", Blockly.JavaScript.ORDER_NONE) || "0") + ");\n"
};

var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

var dx = 0;
var pr = 0;
function plot(data) {
    var hc = 150;
    ctx.strokeStyle = "#f00";
    ctx.lineWidth = 1;
    ctx.moveTo(dx, hc - pr * 60);
    dx++;
    ctx.lineTo(dx, hc - data * 60);
    pr = data;
    ctx.stroke();
}




成果物

以上。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0