def setup
c = createCanvas(100, 100)
background(200)
textAlign(CENTER)
text('drag & drop file', width / 2, height / 2)
c.dragOver do
background(240)
text('Dragged over', width / 2, height / 2)
end
c.dragLeave do
background(200)
text('Dragged off', width / 2, height / 2)
end
c.drop do |file|
background(200)
text('dropped file:', width / 2, height / 2)
text("[#{file.type}]", width / 2, height / 2 + 30)
end
end
def setup
c = createCanvas(100, 100)
background(200)
textAlign(CENTER)
text('drop image', width / 2, height / 2)
c.drop do |file|
@img = createImg(file.data, '').hide # <img>要素を生成
end
end
def draw
if @img
image(@img, 0, 0, width, height) # <img>要素を描画
end
end
ファイルが HTML要素上にドロップされたときに実行する処理を登録します。
| 引数名 | 内容 | 備考 | オプション | デフォルト値 |
|---|---|---|---|---|
| void | 無効化 | falseが指定された場合にはブロックは実行されない (dropメソッドでは効果なし) | ○ | nil |
| ... | ブロック | 実行する処理 |
なし
・ブロック引数'file'にセットされるファイル情報は
HTMLInputElement.filesによって保持されている Fileオブジェクトをベースにしたもので、
以下のような情報を持っています。
| 要素 | 内容 |
|---|---|
| file.name | ファイル名 |
| file.type | MIMEタイプ("image", "text"など) |
| file.subtype | MIMEサブタイプ("png", "plain"など) |
| file.size | ファイルサイズ |
| file.data | データ(ファイルの内容) |