Hướng dẫn get file dimensions javascript - lấy kích thước tệp javascript

I have an image upload box. Standard stuff:


Once the user picks a file, I can access the contents and send it to my API over Axios like such:

const imageUploadInput = document.getElementById["image-upload-input"];
const file = imageUploadInput.files[0];
if [!file] {
  return;
}

const reader = new FileReader[];
const component = this;

reader.onload = function [event] {
  axios.post["upload/image", Buffer.from[event.target.result]];
};
reader.readAsArrayBuffer[file];

However, I have some restrictions regarding dimensions and would like to check those on the client side.

I am fairly certain it can be done. I haven't tried it yet but I imagine if I just pop the data into an [ideally invisible] img tag with a data src attribute, that might work. Does anyone have any working code? Or a better way to do it?

Click to play

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
*{ padding:0px; margin:0px; } html{ width:100%; height:100%; } body{ height:100%; width:100%; } html > body { height: 100%; } #wrapper { width:100%; margin:0 auto; } #wrapper a { text-align:center; } #container { font-size:16px; width:300px; margin:50px auto 0; text-align:center; } #container h2 { color:#276D8C; } #puzzle { height:242px; width:242px; position:relative; border:2px solid #757575; margin:0 auto 25px; } #puzzle .cell-puzzle { width: 58px; height: 58px; border: 2px solid white; position: absolute; text-align: center; vertical-align: middle; line-height: 20px; font-size: 36px; line-height: 60px; background-color: #3B9AFF; z-index: 100; cursor: pointer; user-select: none; } #puzzle .cell-puzzle-zero { background-color:white !important; z-index: 50; } #puzzle > .row-1 { top: 0; } #puzzle > .row-2 { top: 60px; } #puzzle > .row-3 { top: 120px; } #puzzle > .row-4 { top: 180px; } #puzzle > .col-1 { left: 0; } #puzzle > .col-2 { left: 60px; } #puzzle > .col-3 { left: 120px; } #puzzle > .col-4 { left: 180px; } #container-play{ margin: 10px 0; text-align: center; height:50px; } #play{ width:150px; height:50px; border:2px solid #3f4f2e; text-align:center; text-decoration:none; font-weight:600; font-size:20px; background-color:#689b37; display: inline-block; color: white; text-align: center; line-height: 48px; border-radius: 3px; } #alert_puzzle { font-size: 16px; color: coral; }

3. Code tính toán, hiệu ứng của trò chơi bằng Jquery/Javascript:



// ma trận toàn cục điều khiển mọi hoạt động của game
var matrix = [
  [1, 2, 3, 4],
  [5, 6, 7, 8],
  [9, 10, 11, 12],
  [13, 14, 15, 0],
];

// các biến toàn cục dùng để tính giờ
var secs = 0;
var currentSeconds = 0;
var currentMinutes = 0;
var timer;

// gọi sự kiện bắt đầu game khi ấn nút play
$["#play"].bind["click",function[]{
  initialize[];
  beginTime[];
  $["#alert_puzzle"].text[""];
}]

// Khi click vào 1 sô thì gọi đến hàm move để di chuyển
$["#puzzle .cell-puzzle"].bind["click",function[] {
  if[secs > 0]{
    var obj = $[this];
    move[obj];
  }
}]

// hàm init game, gọi 1 dãy số từ 1 đến 15 bất kì, sắp xếp các element theo dãy số đó để bắt đầu game
function initialize[]{
  var arrRandomNumber = shuffleMatrix[];

  var count = 0;
  for[var i = 0; i < 4; i++] {
    for[var j = 0; j < 4; j++] {
      var number = arrRandomNumber[count];
      matrix[i][j] = number;

      if [i == 3 && j == 3] {
        matrix[i][j] = 0;
      }

      $["#puzzle .cell-puzzle[number=" + number + "]"].css["top", i * 60 + "px"];
      $["#puzzle .cell-puzzle[number=" + number + "]"].css["left", j * 60 + "px"];
      count++;
    }
  }
}

// hàm tạo dãy số bất kì từ 1 đến 15
function shuffleMatrix[]{
  var arr = [];
  while[arr.length < 15]{
    var r = Math.floor[Math.random[] * 15] + 1;
    if[arr.indexOf[r] === -1] arr.push[r];
  }

  return arr;
}

// hàm bắt đầu đếm giờ
function beginTime[] {
  secs = 0;
  currentSeconds = 0;
  currentMinutes = 0;
  clearTimeout[timer];
  intervalTime[];
}

// hàm đếm giờ
function intervalTime[] {
  currentMinutes = Math.floor[secs / 60];
  currentSeconds = secs % 60;

  if[currentMinutes  0 && matrix[i - 1][j] == 0] {
          $["#puzzle .cell-puzzle[number=0]"].css["top", i * 60 + "px"];
          obj.animate[{
            'top': [i - 1] * 60 + 'px'
          },300];

          matrix[i-1][j] = numberCell;
          matrix[i][j] = 0;

        } else if[i

Bài Viết Liên Quan

Chủ Đề