# Create an LCD screen with the HTML Canvas

This example shows how to create an LCD screen on a dashboard using the HTML Canvas widget.

#### HTML:

```html
<div id="image_background">
<p>Last value:</p>
<span id="image_background_text">No data</span>
</div>
```

#### CSS:

```css
@import url('https://fonts.googleapis.com/css?family=VT323');

#image_background {
background: url('https://i.imgur.com/uuYygjz.png') 0 / contain no-repeat;
font-family: "VT323";
font-size: 25px;
letter-spacing: 10.5px;
height: 220px;
width: 450px;
}

#image_background p, #image_background_text {
margin: 0;
left: 75px;
position: absolute;
}

#image_background p {
top: 88px;
font-size: 25px !important;
}

#image_background_text {
top: 121px;
}

```

#### JavaScript:

In the following code, set `VAR_ID` to the variable ID to display:

```javascript
var bg = document.getElementById("image_background");
var text = document.getElementById("image_background_text");
var lastValue = null;
var TOKEN = ubidots.token;
var VAR_ID = "variable-id";

setInterval(function () { getLastValue(VAR_ID, TOKEN); }, 2000);

function makeHttpRequest(method, url, headers, payload) {
  return new Promise(function (resolve, reject) {
    var options = {
      method: method,
      headers: headers,
    };

    if (payload) {
      options.body = JSON.stringify(payload);
    }

    fetch(url, options)
      .then(function (response) {
        if (!response.ok) {
          throw new Error(response.statusText);
        }
        return response.json();
      })
      .then(function (data) {
        resolve(data);
      })
      .catch(function (error) {
        reject(error);
      });
  });
}

function changeImage(value) {
  var choose = "normal";
  var background = {
    blue: "https://i.imgur.com/ZE0W7Yx.png",
    normal: "https://i.imgur.com/uuYygjz.png",
    yellow: "https://i.imgur.com/RHvDkUE.png",
  };
  if (value <= 30) {
    choose = "blue";
    bg.style.color = "white";
  } else if (value > 30 && value <= 70) {
    choose = "yellow";
    bg.style.color = "white";
  }
  bg.style.background = "url(" + background[choose] + ") 0 / contain no-repeat";
}

function getLastValue(variableId, token) {
  var url =
    "https://industrial.api.ubidots.com/api/v1.6/variables/" +
    variableId +
    "/values";
  var headers = {
    "X-Auth-Token": token,
    "Content-Type": "application/json",
  };

  makeHttpRequest("GET", url + "?page_size=1", headers)
    .then(function (res) {
      if (lastValue === null || res.results[0].value !== lastValue) {
        text.textContent = res.results[0].value;
        lastValue = res.results[0].value;
        changeImage(lastValue);
      }
    })
    .catch(function (error) {
      console.error(error);
    });
}
```

Once you save the settings, it looks like this:

<figure><img src="/files/AZgdJUoZC48JB3I3KqEE" alt="" width="563"><figcaption></figcaption></figure>

### Adjusting the screen's contrast

Update the HTML to:

```html
<div id="image_background">

<img id="display_background" src="https://i.imgur.com/meoKhMG.png" />

<p>Last value:</p>
<span id="image_background_text">No data</span>
</div>
```

Update the CSS to:

```css
@import url('https://fonts.googleapis.com/css?family=VT323');

#image_background {
background: url('https://i.imgur.com/ehNdUxY.png') 0 / contain no-repeat;
font-family: "VT323";
font-size: 25px;
letter-spacing: 10.5px;
height: 220px;
width: 450px;
}

#image_background p, #image_background_text {
margin: 0;
left: 75px;
position: absolute;
}

#image_background p {
top: 88px;
font-size: 25px !important;
}

#image_background_text {
top: 121px;
}

#display_background {
height: 94px;
position: absolute;
top: 70px;
left: 52px;
pointer-events: none;
}
```

Update the JavaScript to:

```javascript
var bg = document.getElementById("display_background");
var text = document.getElementById("image_background_text");
var lastValue = null;
var lastContrast = null;
var TOKEN = ubidots.token;
var VAR_ID_LV = "id-of-variable-to-be-displayed";
var VAR_ID_CONTRAST = "id-of-contrast-variable";
var scale = d3.scaleLinear().range([0, 4]).domain([0, 1023]);


setInterval(function () {
    // Get LCD Screen last value
    getLastValue(VAR_ID_LV, TOKEN, function (res) {
      var value = null;
      try {
        value = res.results[0].value;
      } catch (e) {
        console.log("No data");
      }
      if (
        (lastValue === null || value !== lastValue) &&
        value !== null
      ) {
        text.textContent = value;
        lastValue = value;
      }
    });

    // Get LCD Screen contrast
    getLastValue(VAR_ID_CONTRAST, TOKEN, function (res) {
      var value = null;
      try {
        value = res.results[0].value;
      } catch (e) {
        console.log("No data");
      }
      if (
        (lastContrast === null || value !== lastContrast) &&
        value !== null
      ) {
        bg.style.filter = "contrast(" + scale(value) + ")";
        lastContrast = value;
      }
    });
  }, 2000);


function makeHttpRequest(method, url, headers, payload) {
  return new Promise(function (resolve, reject) {
    var options = {
      method: method,
      headers: headers,
    };

    if (payload) {
      options.body = JSON.stringify(payload);
    }

    fetch(url, options)
      .then(function (response) {
        if (!response.ok) {
          throw new Error(response.statusText);
        }
        return response.json();
      })
      .then(function (data) {
        resolve(data);
      })
      .catch(function (error) {
        reject(error);
      });
  });
}

function getLastValue(variableId, token, cb) {
  var url =
    "https://industrial.api.ubidots.com/api/v1.6/variables/" +
    variableId +
    "/values";
  var headers = {
    "X-Auth-Token": token,
    "Content-Type": "application/json",
  };

  makeHttpRequest("GET", url + "?page_size=1", headers)
    .then(function (res) {
      if (typeof cb === "function") cb(res);
    })
    .catch(function (error) {
      console.error(error);
    });
}

```

Lastly, add the third-party library required for this implementation:

```
https://cdnjs.cloudflare.com/ajax/libs/d3/4.9.0/d3.min.js
```

Result:

<figure><img src="/files/Fxui4eRZCvarYUqZJQjJ" alt=""><figcaption></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dev.ubidots.com/dashboards-and-widgets/html-canvas/examples/create-an-lcd-screen-with-the-html-canvas.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
