質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Node-RED

Node-REDは、ビジュアルプログラミング向けのフローベース開発ツールです。ブラウザベースのUIになっており、さまざまなノード(Node)を結びつけることでフローを作成でき、処理を実現します。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Q&A

0回答

1175閲覧

自作ノード開発において,jsファイルからhtmlファイル内のscriptタグの関数を叩きたい

muno

総合スコア12

Node-RED

Node-REDは、ビジュアルプログラミング向けのフローベース開発ツールです。ブラウザベースのUIになっており、さまざまなノード(Node)を結びつけることでフローを作成でき、処理を実現します。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

0グッド

1クリップ

投稿2021/03/07 02:25

#やりたいこと
node-red-contrib-browser-utilsのcameraノードの改変を考えています.このノードは,元々ボタンを押したときにPCのカメラから写真を撮影するノードなのですが,これを改変してこのノードになんらかのinputが入ってきた場合に写真を撮影するノードに書き換えたいと思っています.

html

1<!-- 2 Copyright 2013, 2016, 2019 IBM Corp. 3 Licensed under the Apache License, Version 2.0 (the "License"); 4 you may not use this file except in compliance with the License. 5 You may obtain a copy of the License at 6 http://www.apache.org/licenses/LICENSE-2.0 7 Unless required by applicable law or agreed to in writing, software 8 distributed under the License is distributed on an "AS IS" BASIS, 9 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 See the License for the specific language governing permissions and 11 limitations under the License. 12--> 13 14<script type="text/x-red" data-template-name="camera"> 15 <div class="form-row"> 16 <label for="node-input-name"><i class="fa fa-tag"></i> Name</label> 17 <input type="text" id="node-input-name" placeholder="Camera"> 18 </div> 19</script> 20 21<script type="text/x-red" data-help-name="camera"> 22 <p>A simple camera node to capture the current image from the device webcam</p> 23 <p>Usage:</p> 24 <ol> 25 <li>Add the camera node to your flow</li> 26 <li>Click the <code>button</code> capture an image from the webcam</li> 27 </ol> 28 <p>The <code>png</code> image is sent as the <code>msg.payload</code> object</p> 29 <p>Supported browsers</p> 30 <ul> 31 <li>Chrome</li> 32 <li>Firefox</li> 33 </ul> 34</script> 35 36 37<script type="text/javascript"> 38 (function() { 39 navigator.getUserMedia = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia; 40 window.URL = window.URL || window.webkitURL || window.mozURL || window.msURL; 41 42 window.video = document.createElement('video') 43 window.canvas = document.createElement('canvas') 44 canvas.style.display = 'none' 45 canvas.style.display = 'none' 46 var id 47 48 function onCanPlay(evt) { 49 video.removeEventListener('canplay', onCanPlay, false) 50 var width = 320 51 var height = video.videoHeight / (video.videoWidth / width) 52 53 if (isNaN(height)) { 54 height = width / (4 / 3) 55 } 56 57 setTimeout(function(evt) { 58 59 video.setAttribute('width', width) 60 video.setAttribute('height', height) 61 canvas.setAttribute('width', width) 62 canvas.setAttribute('height', height) 63 64 var ctx = canvas.getContext('2d') 65 ctx.drawImage(video, 0, 0, width, height) 66 67 canvas.toBlob(function(blob) { 68 uploadRecord(id, blob) 69 }) 70 }, 2000) 71 } 72 73 function onStreamReady(id, stream) { 74 // As of Chrome 71 createObjectURL is dropped so try direct 75 // access first 76 // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/srcObject 77 try { 78 video.srcObject = stream; 79 } catch (error) { 80 video.src = window.URL.createObjectURL(stream) 81 } 82 83 video.addEventListener('canplay', onCanPlay, false) 84 85 video.play() 86 } 87 88 function onVideoPlaying(id, evt) { 89 canvas.style.height = video.clientHeight + 'px' 90 canvas.style.width = video.clientWidth + 'px' 91 92 var ctx = canvas.getContext('2d') 93 ctx.drawImage(video, 0, 0) 94 95 canvas.toBlob(function(blob) { 96 uploadRecord(id, blob) 97 }) 98 } 99 100 function setCameraStatus(record, id) { 101 $.getJSON('node-red-camera/status', {status: record, id: id}) 102 .done(function () {}) 103 .fail(function (err) { 104 console.log(err); 105 }) 106 .always(function () {}); 107 } 108 109 function takeSnapshot(id) { 110 const constraints = {video : true, audio : false}; 111 navigator.mediaDevices.getUserMedia(constraints) 112 .then(function(stream) { 113 setCameraStatus(true, id) 114 onStreamReady(id, stream) 115 }) 116 .catch(function(err) { 117 window.alert('Your browser does not support the camera node') 118 console.log(err) 119 }) 120 } 121 122 function uploadRecord(id, blob) { 123 var xhr = new XMLHttpRequest() 124 xhr.open('POST', 'node-red-camera/' + id, true) 125 xhr.send(blob) 126 } 127 128 RED.nodes.registerType('camera', { 129 category: 'input', 130 defaults: { 131 name: {value: ''} 132 }, 133 color: 'rgb(215, 201, 194)', 134 inputs: 0, 135 outputs: 1, 136 icon: 'camera.png', 137 paletteLabel: 'camera', 138 label: function() { 139 return this.name || 'camera'; 140 }, 141 labelStyle: function() { 142 return this.name ? 'node_label_italic' : ''; 143 }, 144 button: { 145 onclick: function(){ 146 id = this.id 147 takeSnapshot(id) 148 } 149 } 150 }); 151 })(); 152</script>

Javacript

1/** 2 * Copyright 2013, 2016 IBM Corp. 3 * 4 * Licensed under the Apache License, Version 2.0 (the 'License'); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an 'AS IS' BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 **/ 16 17var bodyParser = require('body-parser') 18 19module.exports = function (RED) { 20 21 function Node (config) { 22 RED.nodes.createNode(this, config); 23 var node = this; 24 var requestSize = '50mb'; 25 26 27 RED.httpAdmin.get('/node-red-camera/status', function (req, res) { 28 var n = RED.nodes.getNode(req.query.id) 29 var status = {}; 30 if ('true' == req.query.status) { 31 status = {fill:'red', shape:'dot', text:'taking picture...'} 32 } 33 if (n) { 34 n.status(status); 35 } 36 res.json({}); 37 }); 38 39 40 RED.httpAdmin.post('/node-red-camera/:id', bodyParser.raw({ type: '*/*', limit: requestSize }), function(req,res) { 41 42 var node = RED.nodes.getNode(req.params.id) 43 44 if (node != null) { 45 try { 46 node.receive({payload: req.body}) 47 node.status({}) 48 res.sendStatus(200) 49 } catch(err) { 50 node.status({fill:'red', shape:'dot', text:'upload failed'}); 51 res.sendStatus(500) 52 node.error(RED._("upload-camera.failed", { error: err.toString() })) 53 } 54 } else { 55 res.status(404).send("no node found") 56 } 57 }) 58 59 this.on('input', function (msg) { 60 if(msg.payload !== '') { 61 node.send(msg) 62 } 63 }) 64 } 65 RED.nodes.registerType('camera', Node) 66};

#試したこと
HTML側に記述されているtakeSnapshotメソッドを,Javascript側の this.on('input', function (msg) {...}内に記載すれば良いと考え,HTML側に記載のある関数や変数群をJavascript側に移行しました.

#試した結果
“ReferenceError: navigator is not defined”とのエラーが発生しました

#考えたこと
あまりこの分野について詳しくないのですが,navigatorはWebブラザに関する物のようなので,HTML側のファイルに記載しないと使えないのかなと思いました

#実現したいこと
Javascript側にtakeSnapshotなどの関数を持ってくるとnavigatorに関するエラーが発生するため,Javascript側からHTML側に記載されたtakeSnapshotを叩くことでinputが入ってきたときに写真を撮影する機能を実現したいです.

また,以上のようなことをしないでもうまくtakeSnapshotをinputが入ってきたときに叩く方法があればご教示いただけると幸いです.

よろしくお願いします.

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問