从画布上的对象显示框

时间:2018-02-05 18:21:30

标签: javascript object canvas

我试图画一个画布2个盒子,但我看不到任何东西。有没有人有想法?

char *receiveData(int sock)
{   
    int counter = 0, i = 0, j = 0;
    char buffer[256];
    bzero(buffer, 256);

    int tempKey = symmetricKeyInt, KeyDigitsNumber = 0;
    while (tempKey > 0)
    {
        tempKey /= 10;
        KeyDigitsNumber++;
    }
    tempKey = symmetricKeyInt;

    int KeyArr[KeyDigitsNumber];
    for (j = KeyDigitsNumber - 1; j >= 0; j--)
    {
        KeyArr[j] = tempKey % 10;
        tempKey = tempKey / 10;
    }

    read(sock, buffer, 255);
    int bufferSize = ntohl(*((int*)&buffer))
    char *temp;
    char *decrypted = malloc(bufferSize);
    char numBuffer[20];
    int testInt;
    int encInt;

    for (j = 0; j < bufferSize; j++)
    {
        bzero(numBuffer, 20);
        read(sock, numBuffer, 19);
        testInt = ntohl(*((int*)&numBuffer)) ^ KeyArr[j %KeyDigitsNumber];
        sprintf(temp, "%d", testInt);
        decrypted[j] = temp;
    }
    printf("THis from inside receiveData function: %s\n", (char *)decrypted);
    return decrypted;
}

1 个答案:

答案 0 :(得分:0)

  • 如果您的元素ID为id="canvas"而非使用getElementById("canvas")
  • 使用ctx作为CanvasRendering的简写上下文(不是canvas
  • 使用ctx.fill()

&#13;
&#13;
var arena = {
  o1: ['gate', 5, 5, 20, 20],
  o2: ['block', 50, 40, 30, 40]
};

$(document).ready(function() {
  var c = document.getElementById('canvas');
  var ctx = c.getContext("2d");

  ctx.lineWidth = "6";
  ctx.strokeStyle = "red";

  var xpercent = c.width / 100;
  var ypercent = c.height / 100;

  for (var key in arena) {
    var arr = arena[key];
    ctx.beginPath();
    ctx.rect(arr[1] * xpercent, arr[2] * ypercent, arr[3] * xpercent, arr[4] * ypercent);
    ctx.fill();
  }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<canvas id="canvas"></canvas>
&#13;
&#13;
&#13;