Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ペースト後にLGTMとLGTNを切り替えられるようにした #22

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions docs/js/app.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,41 @@
'use strict'

$(function (){
$(function () {
var gCanvas = document.querySelector('canvas#output-image')
var gPastedImage = document.querySelector('img#pasted-image')
var gPastedImage = document.querySelector('img#img__lgtn')
const selectedOverlayImageValue = () => document.querySelector('input[name="chooseOverlay"]:checked').value

const setMessage = message => {
const elem = document.querySelector("#paste-area-message")
elem.textContent = message
}

document.addEventListener('paste', (event) => {
$('input[name="chooseOverlay"]').on('change', event => {
if (gPastedImage.src) {
setMessage(`${selectedOverlayImageValue()}画像を再生成しています...`)
redrawLgtnImage()
}
})

document.addEventListener('paste', (event) => {
event.preventDefault()

if (!event.clipboardData
|| !event.clipboardData.types
|| (event.clipboardData.types.length != 1)
|| (event.clipboardData.types[0] != "Files")) {
setMessage('ペーストされたデータが画像ではありません')
setMessage('ペーストされたデータが画像ではありません')
return true;
}

setMessage('LGTN画像を生成しています...')

generateLGTN(event.clipboardData.items[0])

})

const generateLGTN = async function(clipboardItem) {
const generateLGTN = async function (clipboardItem) {
const imageFile = clipboardItem.getAsFile();
const imgEl = document.querySelector("#pasted-image")
const fr = new FileReader();
Expand All @@ -38,9 +48,9 @@ $(function (){

}

const drawCanvas = function() {
const drawCanvas = function () {
const imgEl = document.querySelector("#pasted-image")
const selectedImageId = document.querySelector('input[name="chooseOverlay"]:checked').value
const selectedImageId = selectedOverlayImageValue()
const lgtnEl = document.querySelector(`#img__${selectedImageId}`)
const canvas = document.querySelector('#output-image')
const context = canvas.getContext('2d')
Expand All @@ -52,7 +62,7 @@ $(function (){
let drawY = 0
let drawWidth = canvas.width
let drawHeight = canvas.height
if(imgWidth > imgHeight) {
if (imgWidth > imgHeight) {
// 横の方が長い場合、y座標側を調整する
drawHeight = imgHeight * drawWidth / imgWidth
drawY = (canvas.height - drawHeight) / 2
Expand All @@ -70,18 +80,21 @@ $(function (){

const copyImageToClipboard = (canvas) => {
const pasteArea = document.querySelector('#paste-area')
canvas.toBlob( blob => {
const item = new ClipboardItem({ 'image/png': blob})
canvas.toBlob(blob => {
const item = new ClipboardItem({ 'image/png': blob })
navigator.clipboard.write([item])
.then( () => {
setMessage('クリップボードにLGTN画像がコピーされました!')
.then(() => {
setMessage(`クリップボードに${selectedOverlayImageValue()}画像がコピーされました!`)
})
.catch( ex => {
.catch(ex => {
console.error(ex)
setMessage('クリップボードの書き込み時にエラーが発生しました')
})
});
}

const redrawLgtnImage = () => {
drawCanvas()
}
})