Create a styled-components component that renders a cross inside a recognizable object surrounded by borders

Currently developing an Object Recognition app and aiming to add a border around the object along with a cross that indicates the center. The border has been successfully implemented, but having trouble creating the cross. The plan is to add two more boxes inside, each split in half to form the cross shape. Seeking ideas on how to simplify the process? Utilizing tfjs-models/coco-ssd/ The approach involves dividing the values by 2 for the logic to work

const TargetBox = styled.div`
position: absolute;

left: ${({ x }) => x + "px"};
top: ${({ y }) => y + "px"};
width: ${({ width }) => width + "px"};
height: ${({ height }) => height + "px"};

left2: ${({ x2 }) => x2 / 2+ "px"};
top2: ${({ y2 }) => y2 / 2 + "px"};
width2: ${({ width2 }) => width2 / 2 + "px"};
height2: ${({ height2 }) => height2 / 2 + "px"};

Within the logical calculation section

const normalizePredictions = (predictions, imgSize) => {
if (!predictions || !imgSize || !imageRef) return predictions || [];
return predictions.map((prediction) => {
  const { bbox } = prediction;
  const oldX = bbox[0];
  const oldY = bbox[1];
  const oldWidth = bbox[2];
  const oldHeight = bbox[3];

  const imgWidth = imageRef.current.width;
  const imgHeight = imageRef.current.height;

  const x = (oldX * imgWidth) / imgSize.width;
  const y = (oldY * imgHeight) / imgSize.height;
  const width = (oldWidth * imgWidth) / imgSize.width;
  const height = (oldHeight * imgHeight) / imgSize.height;

  const x2 = (oldX * imgWidth / 2) / imgSize.width;
  const y2 = (oldY * imgHeight / 2) / imgSize.heigh;
  const width2 = (oldWidth * imgWidth / 2) / imgSize.width;
  const height2 = (oldHeight * imgHeight / 2) / imgSize.height;

  return { ...prediction, bbox: [x, y, width, height, x2, y2, width2, height2]

The reference to x2, y2... signifies the box that will be halved. Additional references such as x3, y3... will also be created for another half box. However, currently facing issues with the halves not functioning correctly

What modifications can be made?

Current appearance https://i.sstatic.net/YypwP.png

Desired outcome https://i.sstatic.net/TH1qP.png

Answer №1

You can achieve this by utilizing the before and after pseudo-elements:

.image-container {
  position: relative;
  width: 500px;
}
.image-container img {
  width: 100%;
}
.overlay {
  position: absolute;
  width: 80%;
  height: 80%;
  border: 5px solid green;
  top: 10%;
  left: 10%;
}
.overlay:before {
  content: "";
  height: 5px;
  width: 100%;
  background-color: red;
  position: absolute;
  top: 50%;
  left: 0;
}
.overlay:after {
  content: "";
  height: 100%;
  width: 5px;
  background-color: red;
  position: absolute;
  top: 0;
  left: 50%;
}
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Static Template</title>
  </head>
  <body>
    <div class="image-container">
      <img
        src="https://images.unsplash.com/photo-1573865526739-10659fec78a5"
        alt=""
      />
      <div class="overlay" />
    </div>
  </body>
</html>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Counting checkboxes in jQuery version 1.9

I recently upgraded my website from jQuery 1.6 to jQuery 1.9.1, and now some of my old code is not working as expected. Specifically, I have a piece of code that handles checkboxes in table rows, allowing only up to 5 checkboxes to be active at once and di ...

Redux: Unhandled promise rejection TypeError - Unable to access property 'props' as it is undefined

Currently in the process of learning react and redux with the aim to fetch data from a server and display it on the webpage. However, encountering an error in the process. Any ideas on what might be going wrong? index.js import 'babel-polyfill& ...

Utilizing Firebase 9.0.1 Functions in Vue.js 3

As a newcomer to Vue, I decided to tackle my second tutorial which involved integrating Firebase backend with Vue. However, the tutorial was based on Vue 2 and an older version of Firebase. Determined to stay current, I set out to replicate the project usi ...

Warning: Non-power of two image detected in Three.js

Encountering an issue with a warning in three.js that says: THREE.WebGLRenderer: image is not power of two (600x480). Resized to 512x512. Attempted to resolve it by adding THREE.LinearFilter, but no luck. var texture = new THREE.TextureLoader().load(data[ ...

Tips for avoiding the persistence of an old array on the screen after refreshing and showing the new, updated array

Currently, my task involves displaying array values on a webpage. The array data is sourced from a real-time database in Firebase. After adding new values to the array or inputting another value into the database on the previous page, we are redirected to ...

Encountering an error in React.js: "Unable to access property 'bind' as it is undefined."

Despite binding this.handleChange = this.handleChange.bind(this); within the constructor function, I am still encountering a "cannot read property undefined" error. Is there any solution to fix this issue? import React, { Component } from "react"; import ...

When casting a ray from the inside, the raycast does not collide with the mesh

In my latest project, I've created a unique scene that involves placing my camera inside a sphere geometry. var mat = new THREE.MeshBasicMaterial({map: THREE.ImageUtils.loadTexture('0.jpg') , overdraw:true, color: 0xffffff, wireframe: fal ...

Elevation on Tab button from the upper side

On my website, I have a large form where pressing the tab key switches between input fields. The inputs will also jump to the top if we reach the bottom of the form. The issue is that I have a fixed header at the top, covering the first 60px. Is there a w ...

Is AngularJS Experiencing Bugs with the Webcam getUserMedia API?

I recently created a webcam directive in AngularJS that utilizes a service. For reference, I followed this example: Surprisingly, the example works perfectly on my tablet, but when I integrate the code into my tablet's Google Chrome browser, it encou ...

A React-based frontend solution designed exclusively for managing data structures and databases

Challenges with Product Management In my React App, the shop features products sourced solely from a JSON file as an external API, all managed on the frontend. Recently, I encountered a product with limited availability - only 20 pieces in stock. I am uns ...

Issue with vue.js not recognizing the 'require' function

I am a newcomer to Vue and I am trying to create a basic SPA using Vue without vue-router. Following the example of vue-2.0-simple-routing-example, I am attempting to load pages using require(dynamicPathToFile+'.vue'). However, this approach is n ...

Creating a legitimate string using a function for jqGrid editoptions value: What's the best way to do it?

I am encountering an issue while trying to create a string for the editoptions value using a function. The desired string format is as follows: '1:Active;2:Inactive;3:Pending;4:Suspended'. Strangely, when I manually input this string as the value ...

New methods for Sequelize ES6 models do not currently exist

Encountering issues while using Sequelize JS v4 with ES6 classes, I'm facing difficulty with the execution of instance methods. Despite being defined in the code, these methods appear to be non-existent. For instance - Model File 'use strict&a ...

How to retrieve the latest document from every sender within a JavaScript array using Mongoose/MongoDB queries

I recently created a schema: var messageSchema = mongoose.Schema({ sender:String, recipient:String, content:String, messageType: Number, timestamp: {type: Date, default: Date.now} }); Next, I defined a model for this schema: var Mess ...

What is the process for redirecting an API response to Next.js 13?

Previously, I successfully piped the response of another API call to a Next.js API response like this: export default async function (req, res) { // prevent same site/ obfuscate original API // some logic here fetch(req.body.url).then(r => ...

Creating interactive and adaptable maps

Currently, I am working on making a Joomla website responsive. However, I have encountered an issue with the SobiPro Map. The problem arises when displaying a map (background img) and points (a link + img) over it with an absolute position. As a result, wh ...

Generating Dynamic Widgets Within the Document Object Model

Currently, I am working with jQuery Mobile 1 alpha 1. In order to create a text input field using jQuery Mobile, you need to include the following HTML code: <div data-role='fieldcontain'> <label for='name'>Text Input:</ ...

Javascript time intervals run rapidly during tab changes

I am facing an issue where my neck is constrained at regular time intervals. I have a function that helps me calculate time one second at a time: Header.prototype= { time_changed : function(time){ var that = this; var clock_handle; ...

Conceal a certain element in development using CSS

Is there a way to hide the footer section from my webpage without affecting the rest of the content? For example, using something like: "div class="poweredby" display: none", but I'm unsure of the correct method... Here is the spe ...

Zero's JSON Journey

When I make an HTTP request to a JSON server and store the value in a variable, using console.log() displays all the information from the JSON. However, when I try to use interpolation to display this information in the template, it throws the following er ...